Use python version 3.0 or above
First let's create a window:
import tkinter as tk
root = tk.Tk()
root.title("我的窗口") #窗口标题
root.iconbitmap("my_icon.ico") #窗口图标
root.geometry("500x500+100+100") #窗口尺寸500x500 100+100分别是 X轴Y轴距离
root.resizable(False, False) #窗口禁止放大缩小
root.mainloop() #窗口刷新
A small example of setting up a window on the computer desktop
function | illustrate |
winfo_screenwidth() | screen width |
winfo_screenheight() | screen length |
winfo_reqwidth() | window width |
winfo_reqheight() | window length |
import tkinter as tk
def get_screen_size(win):
return win.winfo_screenwidth(),win.winfo_screenheight()
def center_window(root, width, height):
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
size = '%dx%d+%d+%d' % (width, height, (screenwidth - width)/2, (screenheight - height)/2)
root.geometry(size)
root = tk.Tk()
root.title('居中窗口')
center_window(root, 600, 600)
root.mainloop()
Now that we can create a form, let's get familiar with the commonly used controls.
control | describe |
Button | Button control, similar to Label, except that it provides mouse hover, press, release and keyboard activities/events |
Canvas | Canvas control; displays graphic elements such as lines or text |
CheckButton | Multi-select box control; used to provide multiple selection boxes in the program (similar to Html's checkbox) |
Entry | Input control; used to collect keyboard input |
Frame | Frame control; displays a rectangular area on the screen, mostly used as a container |
Label | Label control; can display text and bitmaps |
Listbox | Listbox control; the Listbox widget is used to display a list of strings to the user |
Menu button | Menu button control, used to display menu items. |
Menu | Menu control; the list of options that pops up after pressing the MenuButton, showing the menu bar, drop-down menu and pop-up menu |
Message | Message control; similar to Label, used to display multiple lines of text, similar to label |
Radiobutton | Radio button control; displays the status of a radio button (similar to Html's radio) |
Scale | Range control; displays a numerical scale that limits the range of numeric intervals for the output |
Scrollbar | Scroll bar control, used when the content exceeds the visual area, provides scrolling function for controls supported by Text, Canvas, Listbox, and Enter. |
Text | Text control; used to display multiple lines of text |
Toplevel | Container control; used to provide a separate dialog box, similar to Frame |
Spinbox | Input control; similar to Entry, but you can specify the input range value |
PanedWindow | PanedWindow is a window layout management plug-in that can contain one or more sub-controls. |
LabelFrame | labelframe is a simple container control. Commonly used and complex window layouts. |
tkMessageBox | Used to display your application's message box. showinfo(title='',message='') |
Next we will operate a demo
import tkinter as tk
#窗体
root = tk.Tk()
root.title("我的窗口")
root.geometry("500x500+300+300")
root.resizable(False, False)
#我们来创建一个button按钮
btn1=tk.Button(root,text='我是按钮1')
btn1.pack()
#也可以写成
btn2=tk.Button(root,text='我是按钮2').pack()
root.mainloop()
The control display setting mode is:
Variable = tk. control (form, text = name, fg = font color, bg = background color, font = (font, size), width = width, height = height)
After the setting is completed, we will show that there will be 3 types: currently starting with pack!
After the setting is completed, the variable .pack() is executed
#我们来创建一个button按钮
btn1=tk.Button(root,text='我是按钮1')
btn1.pack()
#也可以写成
btn2=tk.Button(root, text='我是按钮2', fg='red',bg='blue', font=('黑体',10),width="20",height='5' ).pack()
pack is used as a control display and control positioning parameter
parameter | Internal parameter description |
expand | expand='yes' Level up and down in the play |
fill | fill = 'x': means to fill the entire parent control in the horizontal direction fill = 'y': means to fill the entire parent control in the vertical direction fill = 'both': means to fill the entire parent control |
side | side=tk.TOP represents the top, bottom, left and right of the corresponding form side=tk.BOTTOM side=tk.LEFT side=tk.RIGHT *Similar to float |
anchor | There are 8 directions in total: east, west, south, north, southeast, southwest, northeast, northwest.... They are: e,w,n,s,ne,se,nw,sw Follow-up updates here |
ipadx, ipads | Similar to padding, increasing the space in the X direction or Y direction |
in_ | Python keyword replaces the corresponding text value |
forget() | Hidden: element variable .forget() is displayed corresponding to pack() |
Next we use menu
import tkinter as tk
#窗体
root = tk.Tk()
root.title("我的窗口")
root.geometry("500x500+300+300")
root.resizable(False, False)
menubar=tk.Menu(root)
filemenu=tk.Menu(menubar,tearoff = 0)
filemenu.add_command(label='打开文件')
filemenu1=tk.Menu(menubar,tearoff = 0)
filemenu1.add_command(label='选择编辑文件')
filemenu1.add_command(label='选择搜索文件')
menubar.add_cascade(label='文件', menu=filemenu)
menubar.add_cascade(label='编辑', menu=filemenu1)
menubar.add_cascade(label='Quit',command=root.destroy)
root.config(menu=menubar)
root.mainloop()