[英]Python tkinter frame alignment
How can I place my cb_frame
ideally vertically, aligned to the left? 如何将我的
cb_frame
理想地垂直放置,并向左对齐? When text after some checkbutton has other length than other texts, this checkbutton is then uneven relative to the others. 当某个复选按钮之后的文本的长度比其他文本长时,则此复选按钮相对于其他文本是不均匀的。 I used
place
instead of pack
in cb_frame
because I don't know how to put the frame with checkbuttons in this place by pack
. 我在
cb_frame
使用place
而不是pack
,因为我不知道如何将带有checkbuttons的框架按pack
放在这个位置。
import Tkinter
class MyGUI:
def __init__(self):
self.main_window = Tkinter.Tk()
self.main_window.title("My GUI")
self.main_window.geometry("520x350+500+480")
self.first_frame = Tkinter.Frame()
self.second_frame = Tkinter.Frame()
self.third_frame = Tkinter.Frame()
self.cb_frame = Tkinter.Frame()
self.first_frame.pack(fill="x", padx="3", pady="3")
self.second_frame.pack(fill="x", padx="3", pady="3")
self.third_frame.pack(fill="x", padx="3", pady="3")
self.cb_frame.place(x="220")
self.btn_1 = Tkinter.Button(self.first_frame, text="Button 1", bg="blue", width="12", height="4", command=None)
self.btn_2 = Tkinter.Button(self.first_frame, text="Button 2", bg="blue", width="12", height="4", command=None)
self.btn_3 = Tkinter.Button(self.second_frame, text="Button 3", bg="green", width="12", height="4", command=None)
self.btn_4 = Tkinter.Button(self.second_frame, text="Button 4", bg="green", width="12", height="4", command=None)
self.btn_5 = Tkinter.Button(self.third_frame, text="Button 5", bg="orange", width="12", height="4", command=None)
self.btn_6 = Tkinter.Button(self.third_frame, text="Button 6", bg="orange", width="12", height="4", command=None)
self.btn_1.pack(side="left")
self.btn_2.pack(side="left", padx="2")
self.btn_3.pack(side="left")
self.btn_4.pack(side="left", padx="2")
self.btn_5.pack(side="left")
self.btn_6.pack(side="left", padx="2")
self.cb_var1 = Tkinter.IntVar()
self.cb_var2 = Tkinter.IntVar()
self.cb_var3 = Tkinter.IntVar()
self.cb_var4 = Tkinter.IntVar()
self.cb_var1.set(0)
self.cb_var2.set(0)
self.cb_var3.set(0)
self.cb_var4.set(0)
self.cb1 = Tkinter.Checkbutton(self.cb_frame, text="1000", variable=self.cb_var1).pack()
self.cb2 = Tkinter.Checkbutton(self.cb_frame, text="2000", variable=self.cb_var2).pack()
self.cb3 = Tkinter.Checkbutton(self.cb_frame, text="3000", variable=self.cb_var3).pack()
self.cb4 = Tkinter.Checkbutton(self.cb_frame, text="over 4000", variable=self.cb_var4).pack()
Tkinter.mainloop()
my_gui = MyGUI()
Your cb_frame
is aligned. 您的
cb_frame
已对齐。 It is the checkbuttons that are not aligned. 是未对齐的复选框。 By default they are centered, and you don't override the default.
默认情况下,它们居中,并且您不会覆盖默认值。 A simple fix is to use the
anchor
attribute: 一个简单的解决方法是使用
anchor
属性:
self.cb1 = Tkinter.Checkbutton(...).pack(anchor="w")
self.cb2 = Tkinter.Checkbutton(...).pack(anchor="w")
self.cb3 = Tkinter.Checkbutton(...).pack(anchor="w")
self.cb4 = Tkinter.Checkbutton(...).pack(anchor="w")
By the way, when you do x = Checkbutton(...).pack(...)
, x
will always be None
. 顺便说一句,当您执行
x = Checkbutton(...).pack(...)
, x
始终为None
。 This is because pack(...)
returns None
. 这是因为
pack(...)
返回None
。 You should separate widget creation from widget layout just as matter of good programming, but you must separate them if you need to keep a reference to the widget: 就像良好的编程一样,应该将小部件的创建与小部件的布局分开,但是如果需要保留对小部件的引用,则必须将它们分开:
self.cb1 = Tkinter.Checkbutton(...)
self.cb2 = Tkinter.Checkbutton(...)
...
self.cb1.pack(anchor="w")
self.cb2.pack(anchor="w")
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.