I'm trying to let a button show a table in a new window and add a button to that window to destroy itself. The main problem is that the table shows up in the wrong window. Thanks for help.

I tried to do it myself, but everything I tried doesn't work.


import tkinter as tk

class MainWindow(tk.Frame):
    counter = 0
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.button = tk.Button(self, text="Create new window", 
                                command=self.create_window)
        self.button.grid(padx = 5, pady = 5, row = 1, column = 3)

    def create_window(self):
        height = 2
        width = 3
        numberA=0
        achievs = ["1", "2", "3", "4", "5", "6"]

        self.counter += 1
        t = tk.Toplevel(self)
        t.wm_title("Window #%s" % self.counter)
        for i in range(height): #Rows
            for j in range(width): #Columns
                t = tk.Label(text=achievs[numberA], relief=tk.RIDGE)
                t.grid(row=i, column=j)
                numberA = numberA + 1
        t.mainloop

if __name__ == "__main__":
    root = tk.Tk()
    main = MainWindow(root)
    main.grid(row=4, column=5, padx=5, pady=25)
    root.mainloop()

As said the table should show in the toplevel window and if possible there should be a button to destroy the toplevel window (I probably can do that part myself tough).

1 Answers

0
Gaddo On Best Solutions

Your code has a few mistakes in it, and this seems to be because you might be new to coding, tkinter, or both.

Firstly you are creating a Toplevel() window and assigning it to the variable t (t = tk.Toplevel(self)) and then when you are creating the grid, instead of placing the grid value into the Toplevel window you are assigning the grid value to the variable that hold the Toplevel window

This is not how tkinter works, to add an element to a window you give that element its master, in this case the master of the grid value is the variable t = tk.Toplevel()

This will change the ``create_window` function to be this:

    def create_window(self):
        height = 2
        width = 3
        numberA=0
        achievs = ["1", "2", "3", "4", "5", "6"]

        self.counter += 1
        t = tk.Toplevel(self)
        t.wm_title("Window #%s" % self.counter)
        for i in range(height): #Rows
            for j in range(width): #Columns
                gridVal = tk.Label(t, text=achievs[numberA], relief=tk.RIDGE)
                gridVal.grid(row=i, column=j)
                numberA = numberA + 1

Another error you are making is a standard one for new users of tkinter, that is you are creating a new loop for the Toplevel window.

Only one loop (root.mainloop()) is needed, and creating this second loop will cause problems, so you can just remove the call to create the second loop (t.mainloop)

Changing these things will give you code like this, which should work as you want it to:

import tkinter as tk

class MainWindow(tk.Frame):
    counter = 0
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.button = tk.Button(self, text="Create new window",
                                command=self.create_window)
        self.button.grid(padx = 5, pady = 5, row = 1, column = 3)

    def create_window(self):
        height = 2
        width = 3
        numberA=0
        achievs = ["1", "2", "3", "4", "5", "6"]

        self.counter += 1
        t = tk.Toplevel(self)
        t.wm_title("Window #%s" % self.counter)
        for i in range(height): #Rows
            for j in range(width): #Columns
                gridVal = tk.Label(t, text=achievs[numberA], relief=tk.RIDGE)
                gridVal.grid(row=i, column=j)
                numberA = numberA + 1

if __name__ == "__main__":
    root = tk.Tk()
    main = MainWindow(root)
    main.grid(row=4, column=5, padx=5, pady=25)
    root.mainloop()