How can I place my cb_frame
ideally vertically, aligned to the left? 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
.
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. 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:
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
. This is because pack(...)
returns 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")
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.