Python Forum

Full Version: how to edited Tkinter Toplevel from main form?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i tried to edited Toplevel title, but after 2 hours i failed, please help
from tkinter import *
def editedtoop(top):
    top.title("Edited")

def toop():
    global root
    top = Toplevel(root)
    top.title("toplevel")
    l2 = Label(top, text = "This is toplevel window").pack()
    B = Button(root, text ="Edit Top", command=lambda: editedtoop(top)).pack()
    
root = Tk()
root.title("main")
l = Label(root, text = "This is root window").pack()
toop()
root.mainloop()
Are you trying to do something like this?
from tkinter import Button, Label, Toplevel, Tk

def edit(window):
    window.title('Edited Top Window Title')

def openwindow():
    window = Toplevel(None)
    window.minsize(400,200)
    button = Button(window, text='Edit Top Title')
    button['command'] = lambda window=window: edit(window)
    button.pack()
    

root = Tk()
root.minsize(200,200)
button = Button(root, text='Open Top Level Window')
button['command'] = openwindow
button.pack()
root.mainloop()
(Apr-24-2024, 12:07 PM)menator01 Wrote: [ -> ]Are you trying to do something like this?
from tkinter import Button, Label, Toplevel, Tk

def edit(window):
    window.title('Edited Top Window Title')

def openwindow():
    window = Toplevel(None)
    window.minsize(400,200)
    button = Button(window, text='Edit Top Title')
    button['command'] = lambda window=window: edit(window)
    button.pack()
    

root = Tk()
root.minsize(200,200)
button = Button(root, text='Open Top Level Window')
button['command'] = openwindow
button.pack()
root.mainloop()

thank you, can i ask additional question?
line 7 Window = Toplevel(None), you put None instead root,
does the Toplevel auto get ref to the root?
I'm not sure. I do it that way so root is not the parent window. It is its own parent.
thank you menator01 for the answer, i will give you a reputation point
It doesn't matter if you call Toplevel(), Toplevel(None) or Toplevel(root). The default value for "master" is None. When master is none, the root window is assigned to be the window's master.

This is why one window programs can get away with not passing a master argument when making widgets. tk.Label(text="I am a label") adds a label to the root window, root being the default value for master. This should be avoided because it inevitably leads to confusion when you write a program with two windows or a custom dialog, and the widgets for your second window appear in the root. As seen here:
import tkinter as tk


class BadWindow(tk.Toplevel):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        tk.Label(text="I've been a very bad window!").pack()


class GoodWindow(tk.Toplevel):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        tk.Label(self, text="I follow the rules!").pack()


root = tk.Tk()
tk.Button(root, text="Make a good window.", command=GoodWindow).pack(padx=50, pady=10)
tk.Button(root, text="Make a bad window.", command=BadWindow).pack(padx=50, pady=10)
root.mainloop()
When the program makes a BadWindow the label is added to the root window because no master is specified.
How does the provided example demonstrate the potential confusion that can arise when the "master" argument is not specified correctly?
Run the program and press the buttons. The label made in BadWindow appears in the root window.