I'm having trouble combining tkinter windows/functions, i have simplified my problem down to this which i think seems to be the problem.

Both these functions will work on their own, but not together. (at least the way i want them to!)

What I am after is to be able to press start in Window 1 and have Window 2 open, currently Window 2 will open but it is not displayed properly (s1Var & s2Var dont appear)

Still fairly new to python so it might be something ridiculous :lol:


Thanks.

import Tkinter
 
def win1():
 
    top = Tkinter.Tk()
    top.title("Window 1")
 
    startButton = Tkinter.Button(top, text="Start", command=win2)
    startButton.grid(row=9, column=7)
 
    leaveButton = Tkinter.Button(top, text="Quit", command=top.destroy)
    leaveButton.grid(row=1, column=1, sticky='nw')
    b1Var = Tkinter.StringVar("")
    b2Var = Tkinter.StringVar("")
 
    box1Label = Tkinter.Label(top,text="b1Var",width=12)
    box1Label.grid(row=3, column=2)
 
    box2Label = Tkinter.Label(top,text="b2Var",width=12)
    box2Label.grid(row=3, column=3)
 
    Tkinter.mainloop()
 
def win2():
 
    board = Tkinter.Tk()
    board.title("Window 2")
 
    s1Var = Tkinter.StringVar("")
    s2Var = Tkinter.StringVar("")
 
    s1Var.set("1")
    s2Var.set("2")
 
    square1Label = Tkinter.Label(board,textvariable=s1Var)
    square1Label.grid(row=8, column=7)
 
    square2Label = Tkinter.Label(board,textvariable=s2Var)
    square2Label.grid(row=8, column=6)
    Tkinter.mainloop()

Something like this will work ...

import Tkinter
def win1():
    # this is the main/root window
    root = Tkinter.Tk()
    root.title("Window 1")
    startButton = Tkinter.Button(root, text="Start", command=win2)
    startButton.grid(row=9, column=7)
    leaveButton = Tkinter.Button(root, text="Quit", command=root.destroy)
    leaveButton.grid(row=1, column=1, sticky='nw')
    b1Var = Tkinter.StringVar()
    b2Var = Tkinter.StringVar()
    
    b1Var.set('b1')
    b2Var.set('b2')
    box1Label = Tkinter.Label(root,textvariable=b1Var,width=12)
    box1Label.grid(row=3, column=2)
    box2Label = Tkinter.Label(root,textvariable=b2Var,width=12)
    box2Label.grid(row=3, column=3)
    root.mainloop()

def win2():
    # this is the child window
    board = Tkinter.Toplevel()
    board.title("Window 2")
    s1Var = Tkinter.StringVar()
    s2Var = Tkinter.StringVar()
    s1Var.set("s1")
    s2Var.set("s2")
    square1Label = Tkinter.Label(board,textvariable=s1Var)
    square1Label.grid(row=0, column=7)
    square2Label = Tkinter.Label(board,textvariable=s2Var)
    square2Label.grid(row=0, column=6)

win1()

Renamed 'top' so it less confusing.

commented: Dude you are amazing +0

I tried doing a similar thing.

A function in my program calls a class which opens a new window

class TopLevel3:
     def __init__(self):
        self.root2 = Toplevel()
        self.canvas3 = Canvas(self.root2, width=400, height=200)
        self.canvas3.pack()
        self.root2.title("ImageDB")
        r = 0
        for c in range(6):
            Label(relief=RIDGE,  width=55).grid(row=r, column=0)
            Entry(relief=SUNKEN, width=20).grid(row=r, column=1)
            Button(text='View Image').grid(row=r, column=2)
            Button(text='Update Tag').grid(row=r, column=3)
            r = r+1
        self.root2.mainloop()

This is called by a function in another class:

def img_db(self):
            TopLevel3()

But instead of the labels & buttons appearing in the new child window they appear in the parent window itself.
(Image attached)
Where am I going wrong?

Editor's note:
Please do not hijack an old solved thread for your question. Start your own thread. Many people that could help you will not look in a solved thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.