Basic operations
ImportTkinter as TK#introduction of TK packageWin=tk. Tk ()#Introducing Window ObjectsWin.title ("Window Title")#Window TitleWin.geometry ("200x100")#window width x HeightWin.geometry ("+500+200")#window to the top left of the screenlable=tk. Label (Win,#Parent Window Objecttext='omg! This is tk!',#the text of the labelbg='Green',#Background ColorFont= ('Arial', 12),#font and font sizeWidth=15, height=2#Label Length width )
" "
label or other label settings style can be in string form or in array form
label[' height ']=5
label[' width ']=20
" "
#lable.pack () # Layout in the right place the pack layout is used hereLable.grid ()#layout in the right place the grid layout is used hereWin.mainloop ()#Event Loops
Action Event (command)
# event action Import Tkinter as TK # Click event execution function def P_label ():
global root label =TK. Label (Root,text= " python " ) label.pack () root =TK. Tk () button =tk. button (Root,text= " Click button " , Command=p_label) # Command Click event Command button.pack () Root.mainloop ()
Action Event (BIND)
ImportTkinter as TKdefP_label (events):#must be followed by a parameter GlobalRoot Label= Tk. Label (root,text='I love Python .') label.pack () root=tk. Tk () button= Tk. Button (root,text='Point Me') Button.bind ("<Button-1>", P_label)# MOUSE Click event<Button-1> indicates that the left key 2 means that the wheel 3 is the right buttonButton.pack () root.mainloop ( )
Python Tkinter basic operations and events