前言
今天心情好 更新吧。
今天讲的是按钮,有了它 你的tkinter一定会更进一步
正文
按钮的创建方法为
name=Button(master,text,command,bg,fg,font,width,height,padx,pady,state,highlightcolor,imgage)
上面的参数的作用如下
(以下示例都包含from tkinter import *)
text: 这是按钮上显示的文本。
button = Button(root, text="Click Me!")
command: 当按钮被点击时执行的函数或方法。
def on_click():
print("Button clicked!")
button = Button(root, text="Click Me!", command=on_click)
bg (或 background): 按钮的背景颜色。
button = Button(root, text="Click Me!", bg="red")
fg (或 foreground): 按钮上文本的颜色。
button = Button(root, text="Click Me!", fg="white")
font: 按钮上文本的字体。
button = Button(root, text="Click Me!", font=("Helvetica", 16))
width: 按钮的宽度(以字符数为单位)。
button = Button(root, text="Click", width=10)
height: 按钮的高度(以行数为单位)。
button =Button(root, text="Click", height=3)
padx 和 pady: 按钮内部文本与其边界之间的水平和垂直填充。
button = Button(root, text="Click", padx=10, pady=5)
state: 按钮的状态。可以是 NORMAL、DISABLED 或 ACTIVE。有禁用 正常 什么的
button = Button(root, text="Click", state="disabled")
highlightbackground: 当按钮没有焦点时的高亮边框颜色。
button = Button(root, text="Click", highlightbackground="blue")
highlightcolor: 当按钮有焦点时的高亮边框颜色。
python
button = Button(root, text="Click", highlightcolor="green")
image: 在按钮上显示的图像。
from tkinter import PhotoImage
image = PhotoImage(file="pngname.png")
button = Button(root, image=image)
别忘了创建布局 要不然显示不出来 具体看上一篇
今天怎么什么也想不出来啊!算了 就到这里吧
后言
懒得写
上一篇:python tkinter简介(4)–布局管理
下一篇:python tkinter简介(5)–输入框
注意了 c++不出意外的话要开坑了
还有我的bilibili号也要发视频了
here
here
here
here
here
here
here
here