pytho tkinter application first window

Graphical User Interface ,GUI Graphical User Interface

Python default GUI library Tk (Tk is not the latest and best in the Python GUI library, but Tk is easy to use) The
bottom layer of Tk is written in C++, so the operating efficiency is equivalent to C/C++.
Use 5 steps
1 to import the tkinter module import tkinter
2 to create An instance of the Tk class. The Tk object represents a window.
3 sets the window Venus, such as setting the title of the window through the title method, and setting the size and position of the window through the geometry method.
4. Create an instance of the control class and add the control to the window.
5. Call The mainloop function enters the event loop

# 1导入tkinter模块
import tkinter
# 2创建Tk类的实例,也就是要显示的窗口
window = tkinter.Tk()
# 设置窗口背景颜色
window['background']='blue'
# 定义窗口宽高
w =300
h=200
# tkinter没有让窗口居中的API 所以我们自己设定就好了
# 获取屏幕宽度与高度
ws = window.winfo_screenwidth()
hs = window.winfo_screenheight()
# 根据屏幕宽度与高度计算让窗口垂直居中x,y
x =(ws/2)-(w/2)
y =(hs/2)-(h/2)
# 3 设置窗口标题
window.title('第一个tkinter应用')
# 显示窗口的尺寸位置 使用字符串格式描述 geometry("widthxheight+x+y") 注意这里面的x是xyz的x 不是*
window.geometry('%dx%d+%d+%d'%(w,h,x,y))
# 4 创建Label对象,并将Label放到窗口上,文本显示字符串
label = tkinter.Label(window,text='今晚还得去烧烤庆祝!')
# 使用pack布局让Label居中
label.pack()
# 5 调用mainloop函数进入事件循环
tkinter.mainloop()

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_47021806/article/details/115190036
Recommended