blackSwan566
blackSwan566

Reputation: 1651

Move two windows together tkinter

I have two types of windows: Main and Child. When I move main, all child windows must move also. So I tried to write a method, but I am new to Tkinter so it is a bit hard. Isn't there a mehtod which Tkinter already provides? There are two errors which occure:

line 21, in move_me if second_window != None: NameError: name 'second_window' is not defined

wm_geometry() takes from 1 to 2 positional arguments but 3 were given

''' import tkinter as tk from tkinter import * from tkinter import Tk from functools import partial from tkinter import filedialog import tkinter as tk

root=Tk()

def second_window_X():
    global second_window
    second_window=Tk()
    label=Label(second_window, text='window')
    label.pack()

button=Button(root, text='second window', command=second_window_X)
button.pack()

def move_me(event):
    if second_window != None:
        x = root.winfo_x()
        y = root.winfo_y()
        second_window.geometry(x,y)
root.bind("<Configure>", move_me)

root.mainloop()````

Is there someone who can give me an example how to link both windows togehter and make them move at the same time? And who can explain to me, why move me doesn't knows second_window even if i declared it as global?

Thank you very much already

Sorry for all the imports

Upvotes: 1

Views: 778

Answers (1)

scotty3785
scotty3785

Reputation: 7006

As I suggested in a comment, you shouldn't have two instances of Tk in an application. Your second window should be an instance of Toplevel.

The below code moves the second window when the first window is moved/resized.

from tkinter import *

root=Tk()

second_window = None

def second_window_X():
    global second_window
    second_window=Toplevel(root)
    label=Label(second_window, text='window')
    label.pack()

button=Button(root, text='second window', command=second_window_X,width=100)
button.pack()

def move_me(event):
    try:
        if second_window != None:
            x = root.winfo_x()
            y = root.winfo_y()
            second_window.geometry(f"+{x}+{y}")
    except NameError:
        pass
root.bind("<Configure>", move_me)

root.mainloop()

Upvotes: 1

Related Questions