thinter笔记一

from tkinter import *
root = Tk()          父实例
w = Label(root,text="hello")  
w.pack()
root.mainloop()

from tkinter import *
widget = Label(None,text='hello')   会自动创建TK对象
widget.pack(expand=YES,fill=BOTH)   内容会随着移动,允许标签随着父主件扩展fill=BOTH(水平、垂直),fill=Y(垂直),fill=X(水平)
widget.mainloop()

像字典一样进行显示内容

from tkinter import *
widget = Label()
widget['text']="hello world"
widget.pack(side=TOP)   内容显示在窗口的上面
widget.mainloop()

form tkinter import *
root = Tk()
widget = Label(root)
widget.config(text="hello !我是配置信息")
widget.pack(side=LEFT,expand=YES,fill=BOTH)
root.title('我是标题')
root.mainloop()

添加按钮

from tkinter import *
import sys
root = Tk()
Button(root,text="press",command=sys.exit).pack(side=LEFT)root.mainloop()

自定义回调函数

import sys
from tkinter import *
def quit(): # a custom callback handler
print('Hello, I must be going...') # kill windows and process
sys.exit()
widget = Button(None, text='Hello event world', command=quit)
widget.pack()
widget.mainloop()
import  sys
from tkinter import *
class HelloClass:
    def __init__(self):
        widget = Button(None,text="hello ",command=self.quit)
        widget.pack()
    def quit(self):
        print('hello class')
        sys.exit()
HelloClass()
mainloop()

 绑定事件(Binding Events)

import sys
from tkinter import *
def hello(event):
print('Press twice to exit') # on single-left click 当单击鼠标左键时调用
def quit(event): # on double-left click 对双击鼠标左键时调用
print('Hello, I must be going...') # event gives widget, x/y, etc.
sys.exit()
widget = Button(None, text='Hello event world')
widget.pack()
widget.bind('<Button-1>', hello) # bind left mouse clicks 绑定单击鼠标左键
widget.bind('<Double-1>', quit) # bind double-left clicks 绑定双击鼠标左键
widget.mainloop()

 多组件窗口

from tkinter import *
def greeting():
print('Hello stdout world!...')
win = Frame()
win.pack()
Label(win, text='Hello container world').pack(side=TOP)
Button(win, text='Hello', command=greeting).pack(side=LEFT)
Button(win, text='Quit', command=win.quit).pack(side=RIGHT)
win.mainloop()

posted @ 2016-03-05 14:32  zw~菜园子  阅读(490)  评论(0编辑  收藏  举报