开发者

How to set the width of a Button in tkinter?

I'm trying to make a simple GUI calculator in Python with Tkinter. However the widths of the buttons will often be off by 1 or 2 pixels. Is there any way to force all the buttons to have the same width.

I know of the ipadx method; however, it adds pixels to both sides of the button. This causes problems when the widths of the buttons are odd.

So what's the best method to impose a uniform width?

This is my code:

self.SC.Ca.Btn7 = Tkinter.Button(self.SC.Ca.BtnFrame, text="7", bd=self.SCbw, fg=self.SCfg,
                                 bg=self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.Btn7.grid(column=0, row=0, padx=px, pady=py)

self.SC.Ca.Btn8 = Tkinter.Button(self.SC.Ca.BtnFrame, text="8", bd=self.SCbw, fg=self.SCfg,
                                 bg=self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.Btn8.grid(column=1, row=0, padx=px, pady=py)

self.SC.Ca.Btn9 = Tkinter.Button(self.SC.Ca.BtnFrame, text="9", bd=self.SCbw, fg=self.SCfg,
                                 bg=self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.Btn9.grid(column=2, row=0, padx=px, pady=py)

self.SC.Ca.Btn4 = Tkinter.Button(self.SC.Ca.BtnFrame, text="4", bd=self.SCbw, fg=self.SCfg,
                                 bg=self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.Btn4.grid(column=0, row=1, padx=px, pady=py)

self.SC.Ca.Btn5 = Tkinter.Button(self.SC.Ca.BtnFrame, text="5", bd=self.SCbw, fg=self.SCfg,
                                 bg=self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.Btn5.grid(column=1, row=1, padx=px, pady=py)

self.SC.Ca.Btn6 = Tkinter.Button(self.SC.Ca.BtnFrame, text="6", bd=self.SCbw, fg=self.SCfg,
                                 bg=self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.Btn6.grid(column=2, row=1, padx=px, pady=py)

self.SC.Ca.Btn1 = Tkinter.Button(self.SC.Ca.BtnFrame, text="1", bd=self.SCbw, fg=self.SCfg,
                                 bg=self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.Btn1.grid(column=0, row=2, padx=px, pady=py)

self.SC.Ca.Btn2 = Tkinter.Button(self.SC.Ca.BtnFrame, text="2", bd=self.SCbw, fg=self.SCfg,
                                 bg=self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.Btn2.grid(column=1, row=2, padx=px, pady=py)

self.SC.Ca.Btn3 = Tkinter.Button(self.SC.Ca.BtnFrame, text="3", bd=self.SCbw, fg=self.SCfg,
                                 bg=self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.Btn3.grid(column=2, row=2, padx=px, pady=py)

self.SC.Ca.Btn0 = Tkinter.Button(self.SC.Ca.BtnFrame, text="0", bd=self.SCbw, fg=self.SCfg,
                                 bg=self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.Btn0.grid(column=0, row=3, columnspan=2, ipadx=13, padx=px, pady=py)

self.SC.Ca.BtnPe = Tkinter.Button(self.SC.Ca.BtnFrame, text=".", bd=self.SCbw, fg=self.SCfg,
                                 bg=self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.BtnPe.grid(column=2, row=3, ipadx=2, padx=px, pady=py)



self.SC.Ca.OppBtnFrame = Tkinter.Frame(self.SC.Ca.MainFrame, bd=self.SCbw, bg=self.SCbg)
self.SC.Ca.OppBtnFrame.grid(column=1, row=2)


self.SC.Ca.BtnPN = Tkinter.Button(self.SC.Ca.OppBtnFrame, text="±", bd=self.SCbw, fg=self.SCfg,
                                 bg=开发者_Go百科self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.BtnPN.grid(column=0, row=0, ipadx=4, padx=px, pady=py)

self.SC.Ca.BtnSl = Tkinter.Button(self.SC.Ca.OppBtnFrame, text="/", bd=self.SCbw, fg=self.SCfg,
                                 bg=self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.BtnSl.grid(column=0, row=1, ipadx=3, padx=px, pady=py)

self.SC.Ca.BtnSu = Tkinter.Button(self.SC.Ca.OppBtnFrame, text="-", bd=self.SCbw, fg=self.SCfg,
                                 bg=self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.BtnSu.grid(column=0, row=2, ipadx=2, padx=px, pady=py)

self.SC.Ca.BtnSR = Tkinter.Button(self.SC.Ca.OppBtnFrame, text="√", bd=self.SCbw, fg=self.SCfg,
                                 bg=self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.BtnSR.grid(column=1, row=0, ipadx=1, padx=px, pady=py)



self.SC.Ca.BtnCl = Tkinter.Button(self.SC.Ca.OppBtnFrame, text="C", bd=self.SCbw, fg=self.SCfg,
                                 bg=self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.BtnCl.grid(column=0, row=3, ipadx=0, padx=px, pady=py)

self.SC.Ca.BtnSt = Tkinter.Button(self.SC.Ca.OppBtnFrame, text="*", bd=self.SCbw, fg=self.SCfg,
                                 bg=self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.BtnSt.grid(column=1, row=1, ipadx=1, padx=px, pady=py)

self.SC.Ca.BtnAd = Tkinter.Button(self.SC.Ca.OppBtnFrame, text="+", bd=self.SCbw, fg=self.SCfg,
                                 bg=self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.BtnAd.grid(column=1, row=2, ipadx=1, padx=px, pady=py)

self.SC.Ca.BtnEq = Tkinter.Button(self.SC.Ca.OppBtnFrame, text="=", bd=self.SCbw, fg=self.SCfg,
                                 bg=self.SCbg, activeforeground=self.SCfg, activebackground=self.SCbg, font=self.SCfob)
self.SC.Ca.BtnEq.grid(column=1, row=3, ipadx=1, padx=px, pady=py)


Use the sticky option of the grid geometry manager to "stick" the button to all four sides of its containing cell (i.e. Tkinter.N + Tkinter.S + Tkinter.E + Tkinter.W). If you want to enforce a minimum width then you can either set the button width or better use grid_columnconfigure.

Also, really consider not using copy/paste in building your UI so much - if you've got multiple buttons with the same options, at least put them in a dictionary, use a function or functools.partial. Tkinter makes writing clean UI code very easy, and you'll thank yourself later...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜