tkinter ,wxPython,pyQT
1、
from tkinter import Label
widget=Label(None,text='Hello Gui')
widget.pack()
widget.mainloop()
2| expand fill:組件隨窗口調(diào)整大小
from tkinter import *
widget=Label(None,text='Hello Gui')
widget.pack(expand=YES,fill=BOTH)
widget.mainloop()
file BOTH,Y,X
3舵揭、字典方式設(shè)置組件屬性:
4继效、設(shè)置窗體標(biāo)題
5肛跌、button
import sys
from tkinter import *
w=Button(None,text="tetxxx",command=sys.exit)
w.pack()
w.mainloop()
6忌怎、root.quit,side=LEFT
expand,fill
7捏鱼、自定義回調(diào)函數(shù):
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()
8憔足、類方法
9胁附、綁定鼠標(biāo)事件:<Button-1> <Double-1> bind
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()
10、添加多個(gè)組件:Frame,Button,Label
from tkinter import *
def greeting():
print('Hello stdout world!...')
win = Frame()
win.pack(side=TOP, expand=YES, fill=BOTH)
Button(win, text='Hello', command=greeting).pack(side=LEFT, fill=Y)
Label(win, text='Hello container world').pack(side=TOP)
Button(win, text='Quit', command=win.quit).pack(side=RIGHT, expand=YES,fill=X)
win.mainloop()
11四瘫、fill 當(dāng)窗體擴(kuò)大縮小時(shí)汉嗽,也跟著擴(kuò)大縮小
12、anchor屬性,N,NE,NW,S東西南北
13找蜜、自定義button類:self.init self.__config
from tkinter import *
class HelloButton(Button):
def init(self, parent=None, **config): # add callback method
Button.init(self, parent, **config) # and pack myself
self.pack() # could config style too
self.config(command=self.callback)
def callback(self): # default press action
print('Goodbye world...') # replace in subclasses
self.quit()
if name == 'main':
HelloButton(text='Hello subclass world').mainloop()
13饼暑、self.config fg,bg,font,relief
14、frame重載
from tkinter import *
class Hello(Frame): # an extended Frame
def init(self, parent=None):
Frame.init(self, parent) # do superclass init
self.pack()
self.data = 42
self.make_widgets() # attach widgets to self
def make_widgets(self):
widget = Button(self, text='Hello frame world!', command=self.message)
widget.pack(side=LEFT)
def message(self):
self.data += 1
print('Hello frame world %s!' % self.data)
if name == 'main': Hello().mainloop()
15洗做、Hello.widget() 父類和子類的方法都執(zhí)行
16弓叛、tkinter組件類:
Label,Button,Frame,TK,Message,Entry,Checkbutton,Radiobutton,Scale,PhotoImage,BitmapImage,Menu,Menubutton,Scrollbar,Text,Canvas