Label
界面:
代码:
import tkinter as tk
# 创建主窗口对象,设置窗口大小位置
window = tk.Tk() # 创建主窗口对象
sw = window.winfo_screenwidth() # 获取屏幕宽
sh = window.winfo_screenheight() # 获取屏幕高
cw = 400 # 设置框宽
ch = 400 # 设置框高
window.geometry('%dx%d+%d+%d' % (cw, ch, (sw - cw) / 2, (sh - ch) / 2))
window.title('My Window')
# 设置标签
label = tk.Label(window,
text='Hi,this is Tkinter',
fg='black', bg='green',
width=30, height=2,
font=('Arial', 12))
label.pack()
# 启动循环
window.mainloop() # 启动时间循环
Button
界面:
代码:
import tkinter as tk
# 创建主窗口对象,设置窗口大小位置
window = tk.Tk() # 创建主窗口对象
sw = window.winfo_screenwidth() # 获取屏幕宽
sh = window.winfo_screenheight() # 获取屏幕高
cw = 400 # 设置框宽
ch = 400 # 设置框高
window.geometry('%dx%d+%d+%d' % (cw, ch, (sw - cw) / 2, (sh - ch) / 2))
window.title('My Window')
# 设置按钮
button = tk.Button(window,
text='quit',
# fg='black', bg='blue',
# width=10, height=1,
#font=('Arial', 14),
# anchor='s', # 按钮标签在标签框居中
command=quit # 指令为退出
)
button.pack(side='top') # side设置组件的布局,此处也可以用anchor
# 启动循环
window.mainloop() # 启动时间循环