python tkinter

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è)置組件屬性:


image.png

4、設(shè)置窗體標(biāo)題


image.png

5、button
import sys

from tkinter import *
w=Button(None,text="tetxxx",command=sys.exit)
w.pack()
w.mainloop()
6、root.quit,side=LEFT


image.png

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、類方法


image.png

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


image.png

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í)行

image.png

16、tkinter組件類:
Label,Button,Frame,TK,Message,Entry,Checkbutton,Radiobutton,Scale,PhotoImage,BitmapImage,Menu,Menubutton,Scrollbar,Text,Canvas

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 觸目錢塘闊, 驚濤萬丈樓。 來時(shí)龍虎嘯, 勢(shì)去自低頭。 平水韻 下平十一尤
    蒼浩閱讀 849評(píng)論 16 28
  • 淅淅瀝瀝的小雨斷斷續(xù)續(xù)……據(jù)說這是一年中最冷的幾天。 早晨去找一個(gè)之前在旅行群認(rèn)識(shí)的小伙伴,楊夢(mèng)林,聽起來像是個(gè)女...
    我的蜂蜜呢閱讀 429評(píng)論 2 0
  • 讀了太多的雞湯——讀書能增加人生的厚度,能改變我們的命運(yùn)。也有很多教我們讀書的方法——做筆記,思維導(dǎo)圖,還有一...
    檀一曇閱讀 438評(píng)論 1 7

友情鏈接更多精彩內(nèi)容