我正在尝试使用 tkinter 网格创建一个 ui,以允许用户在两个不同的框架上输入某些值,我希望能够在这两个框架之间进行切换。
我已将不同列中的所有小部件网格化,但是它们都出现在同一列中。
这是我的代码:
class GeneralFrame(ttk.Frame):
def __init__(self, *args):
super().__init__(*args, padding=10, width=1200, height=600)
self.grid_propagate(1)
class EntryBox(GeneralFrame):
def __init__(self, x_location, y_location, *args):
super().__init__(*args)
self.entry_box = ttk.Entry(self)
self.entry_box.place(x=x_location, y=y_location)
print("Placed")
def get_data(self):
return(self.entry_box.get())
class GliderFrame(GeneralFrame):
def __init__(self, *args):
super().__init__(*args)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(2, weight=1)
ttk.Button(self, text="Add Glider", command=self.add_glider).grid(column=0, row=6)
self.polar_coordinate1_label = ttk.Label(text=str("Minimum Sink X Coordinate:"))
self.polar_coordinate2_label = ttk.Label(text=str("Minimum Sink Y Coordinate:"))
self.polar_coordinate3_label = ttk.Label(text=str("Middle Polar X Coordinate:"))
self.polar_coordinate4_label = ttk.Label(text=str("Middle Polar Y Coordinate:"))
self.polar_coordinate5_label = ttk.Label(text=str("VNE X Coordinate:"))
self.polar_coordinate6_label = ttk.Label(text=str("VNE Y Coordinate:"))
self.polar_coordinate1_entry = ttk.Entry(self)
self.polar_coordinate2_entry = ttk.Entry(self)
self.polar_coordinate3_entry = ttk.Entry(self)
self.polar_coordinate4_entry = ttk.Entry(self)
self.polar_coordinate5_entry = ttk.Entry(self)
self.polar_coordinate6_entry = ttk.Entry(self)
def place_widgets(self):
self.polar_coordinate1_entry.grid(column=1, row=0, sticky=tk.E)
self.polar_coordinate2_entry.grid(column=1, row=1, sticky=tk.E)
self.polar_coordinate3_entry.grid(column=1, row=2, sticky=tk.E)
self.polar_coordinate4_entry.grid(column=1, row=3, sticky=tk.E)
self.polar_coordinate5_entry.grid(column=1, row=4, sticky=tk.E)
self.polar_coordinate6_entry.grid(column=1, row=5, sticky=tk.E)
self.polar_coordinate1_label.grid(column=0, row=0, sticky=tk.W)
self.polar_coordinate2_label.grid(column=0, row=1, sticky=tk.W)
self.polar_coordinate3_label.grid(column=0, row=2, sticky=tk.W)
self.polar_coordinate4_label.grid(column=0, row=3, sticky=tk.W)
self.polar_coordinate5_label.grid(column=0, row=4, sticky=tk.W)
self.polar_coordinate6_label.grid(column=0, row=5, sticky=tk.W)
def forget_widgets(self):
self.polar_coordinate1_entry.grid_forget()
self.polar_coordinate2_entry.grid_forget()
self.polar_coordinate3_entry.grid_forget()
self.polar_coordinate4_entry.grid_forget()
self.polar_coordinate5_entry.grid_forget()
self.polar_coordinate6_entry.grid_forget()
self.polar_coordinate1_label.grid_forget()
self.polar_coordinate2_label.grid_forget()
self.polar_coordinate3_label.grid_forget()
self.polar_coordinate4_label.grid_forget()
self.polar_coordinate5_label.grid_forget()
self.polar_coordinate6_label.grid_forget()
def add_glider(self):
polar_coordinates = [[self.polar_coordinate1_entry.get(), self.polar_coordinate2_entry.get()], [self.polar_coordinate3_entry.get(), self.polar_coordinate4_entry.get()], [self.polar_coordinate5_entry.get(), self.polar_coordinate6_entry.get()]]
def plot_polar(self, polar):
fig = Figure(figsize = (5, 5), dpi = 100)
y = [i**2 for i in range(101)]
plot1 = fig.add_subplot(111)
plot1.plot([0,1,2], [1, 2, 3])
canvas = FigureCanvasTkAgg(fig, self)
canvas.show()
canvas.get_tk.grid(side=tk.RIGHT)
class MapFrame(GeneralFrame):
def __init__(self, *args):
super().__init__(*args)
self.turnpoint1_label = ttk.Label(text=str("Turpoint 1:"))
self.turnpoint2_label = ttk.Label(text=str("Turpoint 2:"))
self.turnpoint3_label = ttk.Label(text=str("Turpoint 3:"))
self.turnpoint4_label = ttk.Label(text=str("Turpoint 4:"))
self.turnpoint5_label = ttk.Label(text=str("Turpoint 5:"))
self.pilot_mass_label = ttk.Label(text=str("Pilot Weight:"))
self.turnpoint1_entry = ttk.Entry(self)
self.turnpoint2_entry = ttk.Entry(self)
self.turnpoint3_entry = ttk.Entry(self)
self.turnpoint4_entry = ttk.Entry(self)
self.turnpoint5_entry = ttk.Entry(self)
self.pilot_mass_entry = ttk.Entry(self)
print("Initiated")
def place_widgets(self):
self.turnpoint1_entry.grid(column=1, row=0, sticky=tk.E)
self.turnpoint2_entry.grid(column=1, row=1, sticky=tk.E)
self.turnpoint3_entry.grid(column=1, row=2, sticky=tk.E)
self.turnpoint4_entry.grid(column=1, row=3, sticky=tk.E)
self.turnpoint5_entry.grid(column=1, row=4, sticky=tk.E)
self.pilot_mass_entry.grid(column=1, row=5, sticky=tk.E)
self.turnpoint1_label.grid(column=0, row=0, sticky=tk.W)
self.turnpoint2_label.grid(column=0, row=1, sticky=tk.W)
self.turnpoint3_label.grid(column=0, row=2, sticky=tk.W)
self.turnpoint4_label.grid(column=0, row=3, sticky=tk.W)
self.turnpoint5_label.grid(column=0, row=4, sticky=tk.W)
self.pilot_mass_label.grid(column=0, row=5, sticky=tk.W)
def forget_widgets(self):
self.turnpoint1_entry.grid_forget()
self.turnpoint2_entry.grid_forget()
self.turnpoint3_entry.grid_forget()
self.turnpoint4_entry.grid_forget()
self.turnpoint5_entry.grid_forget()
self.pilot_mass_entry.grid_forget()
self.turnpoint1_label.grid_forget()
self.turnpoint2_label.grid_forget()
self.turnpoint3_label.grid_forget()
self.turnpoint4_label.grid_forget()
self.turnpoint5_label.grid_forget()
self.pilot_mass_label.grid_forget()
class MainFrame(GeneralFrame):
def __init__(self, *args):
super().__init__(*args)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(2, weight=1)
ttk.Button(self, text="Switch Frame", command=self.change_frames).grid(column=2, row=0)
self.MapFrame = MapFrame()
self.GliderFrame = GliderFrame()
self.MapFrame.grid()
self.MapFrame.place_widgets()
self.current_frame = 1
def change_frames(self):
print("Changing Frames")
if self.current_frame == 1:
self.MapFrame.grid_forget()
self.MapFrame.forget_widgets()
self.GliderFrame.grid(column=0, row=0)
self.GliderFrame.place_widgets()
self.current_frame = 2
else:
self.GliderFrame.grid_forget()
self.GliderFrame.forget_widgets()
self.MapFrame.grid(column=0, row=0)
self.MapFrame.place_widgets()
self.current_frame = 1
class App(tk.Tk):
def __init__(self):
super().__init__()
self.geometry("1200x600")
self.title("Gliding Task Analysis")
self.main_frame = MainFrame()
self.main_frame.grid(column=0, row=0)
App().mainloop()
这是输出:
Tkinter 网格未将小部件放置在正确的列中。
我正在尝试使用 tkinter 网格创建一个 ui,以允许用户 在我希望能够的两个不同框架上输入某些值 之间切换
问题可以解决,通过在
MapFrame()
类的 Label 小部件中添加 self 。
还可以通过在第 179 行添加 self.main_frame.grid(column=1,..) 而不是 self.main_frame.grid(column=0,..) 来查看显示的按钮。
MapFrame()
课程片段:
self.turnpoint1_label = ttk.Label(self,text=str("Turpoint 1:"))
self.turnpoint2_label = ttk.Label(self,text=str("Turpoint 2:"))
self.turnpoint3_label = ttk.Label(self,text=str("Turpoint 3:"))
self.turnpoint4_label = ttk.Label(self,text=str("Turpoint 4:"))
self.turnpoint5_label = ttk.Label(self,text=str("Turpoint 5:"))
self.pilot_mass_label = ttk.Label(self,text=str("Pilot Weight:"))
以及在线179:
更改此:
self.main_frame.grid(列=0,行=0)
至:
self.main_frame.grid(列=1,行=0)
截图: