Tkinter之一(TCL/Tk在Python上的移植)
来源:互联网 发布:在线对对联软件 编辑:程序博客网 时间:2024/06/06 04:26
What is Tkinter?
Tkinter是Python的GUI widget。它是基于Tk工具包的,而Tk最初是为了工具命令语言(Tool Command Language, TCL)设计的。由于Tk的流行,它被移植到Perl(Perl/Tk)和Python(Tkinter)。
A minimal application
#!/usr/bin/python
from Tkinter import *
class Application(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.grid()
self.createWidget()
def createWidgets(self):
self.quitButton = Button(self, text = ‘Quit’, command=self.quit)
self.quitButton.grid()
app = Application()
app.master.title(‘Sample application’)
app.mainloop()
from Tkinter import *
class Application(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.grid()
self.createWidget()
def createWidgets(self):
self.quitButton = Button(self, text = ‘Quit’, command=self.quit)
self.quitButton.grid()
app = Application()
app.master.title(‘Sample application’)
app.mainloop()
注意:Application类必须从Frame类继承而来,其__init__函数宜先调用其父类的__init__。其中grid()函数用于将Widget显示出来,感觉应该是跟组件的pack()类似。
另外一种形式的Hello, world
#title: HelloWorld.py
from Tkinter import *
root = Tk()
label = Label(text = “hello world”);
label.pack()
root.mainloop()
from Tkinter import *
root = Tk()
label = Label(text = “hello world”);
label.pack()
root.mainloop()
Layout management
尽管在Tkinter中有三种geometry managers,但是本文将使用.grid()。这种Layout管理器将所有的widget都看成是一个由列跟行组成的表格。通常包含如下术语:
- 单元cell
- 宽width
- 高 height
- extra space
- 合并单元格spanning
创建了一个widget后,只有将其向geometry manager注册后,该widget才会显示出来。
thing = Constructor(master, …)
thing.grid(…)
thing.grid(…)
用类封装的Hello, world例子。
#file : hello2.py
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(frame, text = ‘Quit’, fg =’red’, command = frame.quit)
self.button.pack(side = LEFT)
self.hi_there = Button(frame, text = ‘Hello’, command = self.say_hi)
self.hi_there.pack()
def say_hi(self):
print ‘hi there, everyone!’
root = Tk()
app = App(root)
root.mainloop()
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(frame, text = ‘Quit’, fg =’red’, command = frame.quit)
self.button.pack(side = LEFT)
self.hi_there = Button(frame, text = ‘Hello’, command = self.say_hi)
self.hi_there.pack()
def say_hi(self):
print ‘hi there, everyone!’
root = Tk()
app = App(root)
root.mainloop()
- Tkinter之一(TCL/Tk在Python上的移植)
- Tkinter之二(TCL/Tk在Python上的移植)
- Tkinter之四(TCL/Tk在Python上的移植)
- Tk:perl/Tk, TCL/Tk, python/Tk(Tkinter)示例
- tcl/tk and python
- ubuntu12.04.4安装tcl/tk和Tkinter
- tcl/tk的安装
- tcl/tk的安装
- tcl/tk 的安装
- Tcl/Tk入门(上)
- python的tkinter可以更换红色的图标“Tk”
- TCL/tk arm 移植
- Tcl的历史--Tcl/Tk简介
- Tcl/Tk的一些笔记
- Tcl/Tk的一些笔记
- Tcl/TK -- upvar 的用法
- linux上安装tcl、tk、expect
- linux上安装tcl、tk、expect
- 有关美国精神的三个小故事
- 敏捷项目管理(摘录)——指导原则
- 盛大之极品面试
- 资深猎头眼中的优质简历是如何炼成的?
- 2.18 C#
- Tkinter之一(TCL/Tk在Python上的移植)
- 编辑日志和删除日志都得有权限才能进行
- linux解压 tar命令
- Google手机遭遇成长的烦恼
- 百位数精确求和
- 学习《深入浅出MFC》总结(上)
- 博客首篇
- 开心一下
- 1+1猜想 II