我正在尝试使用Tkinter在Python中创建一个简单的GUI计算器。但是,按钮的宽度通常会偏离1或2像素。有没有办法强制所有按钮具有相同的宽度。
我知道 ipadx 方法;但是,它会在按钮的两侧添加像素。当按钮的宽度为奇数时,这会导致问题。
那么施加均匀宽度的最佳方法是什么?
这是我的代码:
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=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)
答案 0 :(得分:3)
使用sticky
几何管理器的grid
选项将按钮“粘贴”到其包含单元格的所有四个边(即Tkinter.N + Tkinter.S + Tkinter.E + Tkinter.W
)。如果要强制实施最小宽度,则可以设置按钮宽度或更好地使用grid_columnconfigure
。
另外,真的考虑不要在构建你的UI时使用复制/粘贴 - 如果你有多个具有相同选项的按钮,至少将它们放在字典中,使用函数或functools.partial
。 Tkinter使得编写干净的UI代码非常容易,以后你会感谢自己...