tkinker 组件 Button
2022-01-10 本文已影响0人
HC2
Button(按钮)组件用于实现各种各样的按钮。Button 组件可以包含文本或图像,你可以将一个 Python 的函数或方法与之相关联,当按钮被按下时,对应的函数或方法将被自动执行。
一、
import tkinter as tk
# 建立窗口
def A():
print("按钮被点击")
window = tk.Tk()
window.title('hello thinter')
height= window.winfo_screenheight()
width= window.winfo_screenwidth()
window.geometry('400x200+%d+%d'%((width-400)/2,(height-200)/2))
tk.Button(window,text="按钮",command=A).pack()
window.mainloop()
二、按钮设置大小
import tkinter as tk
# 建立窗口
def A():
print("按钮被点击")
window = tk.Tk()
window.title('hello thinter')
height= window.winfo_screenheight()
width= window.winfo_screenwidth()
window.geometry('400x200+%d+%d'%((width-400)/2,(height-200)/2))
tk.Button(window,text="按钮",width=10,height=10,command=A).pack()
window.mainloop()
三、禁止点击
import tkinter as tk
# 建立窗口
def A():
print("按钮被点击")
window = tk.Tk()
window.title('hello thinter')
height= window.winfo_screenheight()
width= window.winfo_screenwidth()
window.geometry('400x200+%d+%d'%((width-400)/2,(height-200)/2))
tk.Button(window,text="按钮",width=10,height=10,command=A,state='disabled').pack()
window.mainloop()
参数
Button(master=None, **options) (class)
master -- 父组件
**options -- 组件选项,下方表格详细列举了各个选项的具体含义和用法:
image.png image.png image.png image.png方法
flash()
-- 刷新 Button 组件,该方法将重绘 Button 组件若干次(在 "active" 和 "normal" 状态间切换)。
invoke()
-- 调用 Button 中 command 选项指定的函数或方法,并返回函数的返回值。
-- 如果 Button 的state(状态)是 "disabled"(不可用)或没有指定 command 选项,则该方法无效。