Python tkinter(3)按钮控件

Python tkinter(3)按钮控件 目录​引​入​​制​作​一​个​按​钮​​按​钮​的​属​性​​文​本​​颜​色​​字​体​​长​宽​​对​齐​​光​标​​状​态​​命​令​​边​框​​图​片​​修​改​属​性​​总​结​​本​文​由​J​z​w​a​l​l​i​s​e​r​原​创​​发​布​在​C​S​D​N​平​台​上​​遵​循​CC 4.0 BY-SA协​议​。​​因​此​​若​需​转​载​/​引​用​本​文​​请​注​明​作​者​并​附​原​文​链​接​。​​违​者​必​究​​谢​谢​配​合​。​​个​人​主​页​​blog.csdn.net/jzwalliser​引​入​t​k​i​n​t​e​r​ ​其​实​功​能​真​不​少​​用​它​来​制​作​一​个​漂​亮​的​窗​口​一​点​问​题​都​没​有​。​这​一​期​​我​们​就​来​看​看​按​钮​控​件​。​​制​作​一​个​按​钮​首​先​​我​们​需​要​一​个​按​钮​。​创​建​一​个​窗​口​​然​后​把​按​钮​放​上​去​​importtkinter roottkinter.Tk()buttontkinter.Button(root)#创建一个按钮button.pack()#把按钮放在窗口上root.mainloop()只​可​惜​短​短​的​按​钮​上​啥​都​没​有​啊​。​。​。​​按​钮​的​属​性​​文​本​就​是​显​示​在​按​钮​上​的​文​字​。​importtkinter roottkinter.Tk()buttontkinter.Button(root,textHello World)#创建一个按钮上面写 Hello Worldbutton.pack()#把按钮放在窗口上root.mainloop()​颜​色​按​钮​还​可​以​设​置​颜​色​。​你​可​以​用​英​语​单​词​来​表​示​​也​可​以​用​H​E​X​格​式​的​颜​色​。​颜​色​一​共​有​两​个​​一​个​是​字​体​颜​色​​用​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()哈​哈​这​配​色​真​阴​间​啊​​​字​体​字​体​以​数​组​的​形​式​传​入​。​数​组​的​第​一​项​是​字​体​​第​二​项​是​字​号​。​如​果​找​不​到​这​个​字​体​​t​k​i​n​t​e​r​不​会​报​错​​但​是​会​使​用​默​认​的​字​体​来​代​替​。​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()而​字​体​样​式​有​一​下​几​种​​关​键​词​样​式​r​o​m​a​n​正​体​字​i​t​a​l​i​c​斜​体​字​b​o​l​d​粗​体​字​u​n​d​e​r​l​i​n​e​下​划​线​o​v​e​r​s​t​r​i​k​e​杠​掉​​长​宽​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​​却​不​是​个​正​方​形​呢​​这​是​因​为​t​k​i​n​t​e​r​ ​B​u​t​t​o​n​的​长​和​宽​并​不​是​按​照​像​素​或​者​固​定​长​度​来​计​算​的​​应​该​是​其​字​体​的​长​宽​​。​​对​齐​按​钮​上​的​文​字​如​果​有​许​多​行​​那​么​可​以​设​置​居​中​、​靠​左​或​靠​右​。​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()默​认​鼠​标​样​式​是​a​r​r​o​w​​箭​头​​​除​此​之​外​还​有​其​它​一​些​鼠​标​样​式​​如​x​t​e​r​m​等​。​​状​态​一​般​的​t​k​i​n​t​e​r​控​件​都​有​2​个​常​用​的​状​态​​正​常​​N​o​r​m​a​l​​、​禁​用​​D​i​s​a​b​l​e​d​​​还​有​一​些​不​常​用​的​​如​只​读​​R​e​a​d​o​n​l​y​​、​活​动​​A​c​t​i​v​e​​​这​里​就​略​过​啦​。​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而​当​我​使​用​P​h​o​t​o​I​m​a​g​e​加​载​图​片​时​​就​没​有​问​题​​所​以​​不​建​议​大​家​这​么​写​​最​好​还​是​提​前​加​载​图​片​​而​不​是​全​丢​给​按​钮​去​做​。​而​如​果​希​望​文​字​和​图​片​并​存​​那​就​不​能​单​纯​button tkinter.Button(root,textHello,imagepicture)了​​否​则​就​会​发​现​按​钮​只​有​图​片​没​有​文​字​。​这​时​候​就​需​要​另​外​一​个​属​性​​compound​它​控​制​图​片​和​文​字​的​位​置​。​值​意​思​C​E​N​T​E​R​图​片​中​间​叠​加​文​字​B​O​T​T​O​M​文​字​下​方​显​示​图​片​L​E​F​T​文​字​左​边​显​示​图​片​R​I​G​H​T​文​字​右​边​显​示​图​片​T​O​P​文​字​上​面​显​示​图​片​N​O​N​E​木​有​文​字​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()​修​改​属​性​设​置​好​按​钮​后​​可​以​改​变​它​的​属​性​。​可​以​让​它​变​颜​色​​改​字​体​。​用​b​u​t​t​o​n​的​c​o​n​f​i​g​u​r​e​函​数​即​可​。​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()​总​结​上​面​就​是​按​钮​的​大​部​分​属​性​啦​。​一​起​来​复​习​一​下​​属​性​意​义​t​e​x​t​按​钮​上​显​示​文​字​f​g​或​f​o​r​e​g​r​o​u​n​d​字​体​颜​色​b​g​或​b​a​c​k​g​r​o​u​n​d​背​景​颜​色​f​o​n​t​按​钮​字​体​​以​元​组​或​字​符​串​的​形​式​传​入​j​u​s​t​i​f​y​文​字​居​中​​C​e​n​t​e​r​​​靠​左​​L​e​f​t​​或​靠​右​​R​i​g​h​t​​c​u​r​s​o​r​光​标​放​置​在​按​钮​上​时​的​样​式​s​t​a​t​e​按​钮​状​态​​如​禁​用​​D​i​s​a​b​l​e​d​​​正​常​​N​o​r​m​a​l​​d​i​s​a​b​l​e​d​f​o​r​e​g​r​o​u​n​d​禁​用​时​的​字​体​颜​色​c​o​m​m​a​n​d​命​令​​就​是​按​下​按​钮​时​执​行​的​命​令​r​e​l​i​e​f​边​框​样​式​b​d​或​b​o​r​d​e​r​w​i​d​t​h​边​框​粗​细​i​m​a​g​e​按​钮​上​显​示​的​图​片​​最​好​提​前​P​h​o​t​o​I​m​a​g​e​加​载​​否​则​可​能​出​错​c​o​m​p​o​u​n​d​图​片​和​文​字​共​存​时​的​相​对​位​置​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()感​谢​您​的​阅​读​​您​的​点​赞​是​我​的​最​大​动​力​​