Tkinter Grid Not Placing Widgets in Correct Columns

80 Views Asked by At

I'm trying to create a ui using a tkinter grid to allow the user to enter certain values on two different frames that I want to be able to switch between.

I have grided all the widgets in different column's however, they are all appearing in the same column.

Here is my code:

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()

Here is the output:

(https://i.stack.imgur.com/bQfkz.png)

1

There are 1 best solutions below

0
toyota Supra On

Tkinter Grid Not Placing Widgets in Correct Columns.

I'm trying to create a ui using a tkinter grid to allow the user to enter certain values on two different frames that I want to be able to switch between

The problem can be solved, By adding self in Label widget in MapFrame() class.

Also to see Button on display by adding self.main_frame.grid(column=1,..) instead of self.main_frame.grid(column=0,..) on line 179.

Snippet on MapFrame() class:

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:"))

And on line 179:

change this:

self.main_frame.grid(column=0, row=0)

to:

self.main_frame.grid(column=1, row=0)

Screenshot:

enter image description here