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:


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)
def win2():
    board = Tkinter.Tk()
    board.title("Window 2")
    s1Var = Tkinter.StringVar("")
    s2Var = Tkinter.StringVar("")
    square1Label = Tkinter.Label(board,textvariable=s1Var)
    square1Label.grid(row=8, column=7)
    square2Label = Tkinter.Label(board,textvariable=s2Var)
    square2Label.grid(row=8, column=6)

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

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


Renamed 'top' so it less confusing.

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

This is called by a function in another class:

def img_db(self):

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.

Edited 6 Years Ago by vegaseat: hijack

This question has already been answered. Start a new discussion instead.