按钮控件)
目录引入制作一个按钮按钮的属性文本颜色字体长宽对齐光标状态命令边框图片修改属性总结本文由Jzwalliser原创发布在CSDN平台上遵循CC 4.0 BY-SA协议。因此若需转载/引用本文请注明作者并附原文链接。违者必究谢谢配合。个人主页blog.csdn.net/jzwalliser引入tkinter 其实功能真不少用它来制作一个漂亮的窗口一点问题都没有。这一期我们就来看看按钮控件。制作一个按钮首先我们需要一个按钮。创建一个窗口然后把按钮放上去importtkinter roottkinter.Tk()buttontkinter.Button(root)#创建一个按钮button.pack()#把按钮放在窗口上root.mainloop()只可惜短短的按钮上啥都没有啊。。。按钮的属性文本就是显示在按钮上的文字。importtkinter roottkinter.Tk()buttontkinter.Button(root,textHello World)#创建一个按钮上面写 Hello Worldbutton.pack()#把按钮放在窗口上root.mainloop()颜色按钮还可以设置颜色。你可以用英语单词来表示也可以用HEX格式的颜色。颜色一共有两个一个是字体颜色用fg或foreground作为属性的名称还有一个是背景颜色用bg或background作为属性名称。importtkinter roottkinter.Tk()button1tkinter.Button(root,textHi!,fgred,bgblue)#红色字体蓝色背景用英语单词代替颜色button1.pack()#把按钮放在窗口上button2tkinter.Button(root,textHi!,fg#00ff00,bg#0000ff)#绿色字体蓝色背景用HEX格式的颜色button2.pack()#其实这么写也可以#button2 tkinter.Button(root,textHi!,foreground#00ff00,background#0000ff)#但这样写有点麻烦你觉得呢root.mainloop()哈哈这配色真阴间啊字体字体以数组的形式传入。数组的第一项是字体第二项是字号。如果找不到这个字体tkinter不会报错但是会使用默认的字体来代替。importtkinter roottkinter.Tk()buttontkinter.Button(root,textHi!,font(Arial,50,roman))#字体为Arial字号为50正体字也可以不指定样式如font(Arial,50)#若不需要设置字体大小和字体样式也可以直接写为#button tkinter.Button(root,fontArial)button.pack()#把按钮放在窗口上root.mainloop()而字体样式有一下几种关键词样式roman正体字italic斜体字bold粗体字underline下划线overstrike杠掉长宽importtkinter roottkinter.Tk()button1tkinter.Button(root,textHi!,width5)#宽为5button1.pack()#把按钮放在窗口上button2tkinter.Button(root,textHi!,height5)#长为5button2.pack()#把按钮放在窗口上button3tkinter.Button(root,textHi!,height5,width5)#长宽都为5button3.pack()#把按钮放在窗口上root.mainloop()哎真有点丑啊。。。但为什么我最后一个按钮明明设置的是长宽都为5却不是个正方形呢这是因为tkinter Button的长和宽并不是按照像素或者固定长度来计算的应该是其字体的长宽。对齐按钮上的文字如果有许多行那么可以设置居中、靠左或靠右。importtkinter roottkinter.Tk()string水调歌头 【宋】 苏轼 丙辰中秋欢饮达旦大醉作此篇兼怀子由。 明月几时有把酒问青天。不知天上宫阙今夕是何年。我欲乘风归去又恐琼楼玉宇高处不胜寒。起舞弄清影何似在人间。 转朱阁低绮户照无眠。不应有恨何事长向别时圆人有悲欢离合月有阴晴圆缺此事古难全。但愿人长久千里共婵娟。 button1tkinter.Button(root,textstring,justifytkinter.LEFT)#靠左button1.pack()#把按钮放在窗口上button2tkinter.Button(root,textstring,justifytkinter.RIGHT)#靠右button2.pack()#把按钮放在窗口上button3tkinter.Button(root,textstring,justifytkinter.CENTER)#居中button3.pack()#把按钮放在窗口上root.mainloop()光标光标有许多种样式。内容不少这里就稍微介绍下吧其余的内容我找时间再专门写一篇吧。importtkinter roottkinter.Tk()buttontkinter.Button(root,textHi!,cursorwatch)#光标放在按钮上后转圈button.pack()#把按钮粘到窗口上root.mainloop()默认鼠标样式是arrow箭头除此之外还有其它一些鼠标样式如xterm等。状态一般的tkinter控件都有2个常用的状态正常Normal、禁用Disabled还有一些不常用的如只读Readonly、活动Active这里就略过啦。importtkinter roottkinter.Tk()button1tkinter.Button(root,textNormal,statetkinter.NORMAL)#正常button1.pack()#把按钮放在窗口上button2tkinter.Button(root,textDisabled,statetkinter.DISABLED)#禁用无法点击按钮button2.pack()#把按钮放在窗口上root.mainloop()被禁用的按钮在默认情况下字体是灰色的而且点不下去。但是也可以设置禁用时的字体颜色disabledforeground用法和属性fg类似但似乎不能设置禁用时的背景色。如buttontkinter.Button(root,textDisabled,statetkinter.DISABLED,disabledforegroundgreen)#禁用时的字体是绿油油的命令按下按钮后需要有什么功能呢这可是按钮的灵魂啊。这时候属性command就派上用场了。importtkinterdefclick():print(Hello World!)roottkinter.Tk()buttontkinter.Button(root,textClick me!,commandclick)#点击之后运行先前定义的click函数button.pack()#把按钮放在窗口上root.mainloop()边框边框一共有这么几种flat、groove、raised、ridge、solid、sunken就一起来看看效果吧importtkinter roottkinter.Tk()relief[flat,groove,raised,ridge,solid,sunken]#不同的样式foriinrelief:buttontkinter.Button(root,texti,reliefi)#每个样式来一个按钮button.pack()#把每个样式的按钮放在窗口上root.mainloop()有了边框样式我们还可以设置边框的厚度bd或borderwidth。importtkinter roottkinter.Tk()buttontkinter.Button(root,textHi!,bd20)#厚厚的一层边框button.pack()#把按钮放在窗口上root.mainloop()图片如果一个按钮上只有文字那未免太无聊了。我们还可以在按钮上放张图片importtkinter roottkinter.Tk()phototkinter.PhotoImage(filelaugh.png)#加载一张图片buttontkinter.Button(root,imagephoto)#设置图片button.pack()#把按钮贴在窗口上root.mainloop()其实这么写应该也可以button tkinter.Button(root,imagelaugh.png)但是总会出现一些莫名其妙的报错如Traceback (most recent call last): File stdin, line 1, in module File /usr/lib/python3.8/tkinter/__init__.py, line 2650, in __init__ Widget.__init__(self, master, button, cnf, kw) File /usr/lib/python3.8/tkinter/__init__.py, line 2572, in __init__ self.tk.call( _tkinter.TclError: image laugh.png doesnt exist而当我使用PhotoImage加载图片时就没有问题所以不建议大家这么写最好还是提前加载图片而不是全丢给按钮去做。而如果希望文字和图片并存那就不能单纯button tkinter.Button(root,textHello,imagepicture)了否则就会发现按钮只有图片没有文字。这时候就需要另外一个属性compound它控制图片和文字的位置。值意思CENTER图片中间叠加文字BOTTOM文字下方显示图片LEFT文字左边显示图片RIGHT文字右边显示图片TOP文字上面显示图片NONE木有文字importtkinter roottkinter.Tk()phototkinter.PhotoImage(filelaugh.png)#加载一张图片compound[tkinter.CENTER,tkinter.BOTTOM,tkinter.LEFT,tkinter.RIGHT,tkinter.TOP,tkinter.NONE]foriinrange(len(compound)):buttontkinter.Button(root,imagephoto,textcompound[i],compoundcompound[i])#按钮button.grid(rowint(i/2),columni%2)#把按钮贴在窗口上root.mainloop()修改属性设置好按钮后可以改变它的属性。可以让它变颜色改字体。用button的configure函数即可。importtkinter roottkinter.Tk()buttontkinter.Button(root,imagephoto,textHello!)#按钮button.pack()#把按钮贴在窗口上button.configure(textHi!,font(Consolas,50))#将按钮上的文字改为Hi!再将字体设置为Consolas字体大小50#configure 和 config 都可以#button.config(textHi!,font(Consolas,50)) 效果一样root.mainloop()总结上面就是按钮的大部分属性啦。一起来复习一下属性意义text按钮上显示文字fg或foreground字体颜色bg或background背景颜色font按钮字体以元组或字符串的形式传入justify文字居中Center靠左Left或靠右Rightcursor光标放置在按钮上时的样式state按钮状态如禁用Disabled正常Normaldisabledforeground禁用时的字体颜色command命令就是按下按钮时执行的命令relief边框样式bd或borderwidth边框粗细image按钮上显示的图片最好提前PhotoImage加载否则可能出错compound图片和文字共存时的相对位置importtkinter roottkinter.Tk()buttontkinter.Button(root,textHello World)#创建一个按钮上面写 Hello Worldbuttontkinter.Button(root,textHi!,fgred,bgblue)#红色字体蓝色背景用英语单词代替颜色buttontkinter.Button(root,textHi!,fg#00ff00,bg#0000ff)#绿色字体蓝色背景用HEX格式的颜色buttontkinter.Button(root,textHi!,font(Arial,50,roman))#字体为Arial字号为50正题字buttontkinter.Button(root,fontArial)#字体Arial默认字号默认字体样式buttontkinter.Button(root,textHi!,width5)#宽为5buttontkinter.Button(root,textHi!,height5)#长为5buttontkinter.Button(root,textHi!,height5,width5)#长宽都为5string水调歌头 【宋】 苏轼 丙辰中秋欢饮达旦大醉作此篇兼怀子由。 明月几时有把酒问青天。不知天上宫阙今夕是何年。我欲乘风归去又恐琼楼玉宇高处不胜寒。起舞弄清影何似在人间。 转朱阁低绮户照无眠。不应有恨何事长向别时圆人有悲欢离合月有阴晴圆缺此事古难全。但愿人长久千里共婵娟。 buttontkinter.Button(root,textstring,justifytkinter.LEFT)#靠左buttontkinter.Button(root,textstring,justifytkinter.RIGHT)#靠右buttontkinter.Button(root,textstring,justifytkinter.CENTER)#居中buttontkinter.Button(root,textHi!,cursorwatch)#光标放在按钮上后转圈buttontkinter.Button(root,textNormal,statetkinter.NORMAL)#正常buttontkinter.Button(root,textDisabled,statetkinter.DISABLED)#禁用无法点击按钮buttontkinter.Button(root,textDisabled,statetkinter.DISABLED,disabledforegroundgreen)#禁用时的字体是绿油油的defclick():print(Hello World!)buttontkinter.Button(root,textClick me!,commandclick)#点击之后运行先前定义的click函数buttontkinter.Button(root,textGroove,reliefgroove)#不同边框样式的按钮buttontkinter.Button(root,textHi!,bd20)#厚厚的一层边框phototkinter.PhotoImage(filelaugh.png)#加载一张图片buttontkinter.Button(root,imagephoto)#设置图片buttontkinter.Button(root,imagephoto,textCenter,compoundtkinter.CENTER)#图片位置button.configure(textHi!,font(Consolas,50,roman))button.pack()#按钮放在窗口上root.mainloop()感谢您的阅读您的点赞是我的最大动力