Python Forum
how to edited Tkinter Toplevel from main form?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to edited Tkinter Toplevel from main form?
#1
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()
Reply
#2
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()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(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?
Reply
#4
I'm not sure. I do it that way so root is not the parent window. It is its own parent.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
thank you menator01 for the answer, i will give you a reputation point
menator01 likes this post
Reply
#6
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.
Reply
#7
How does the provided example demonstrate the potential confusion that can arise when the "master" argument is not specified correctly?
Reply
#8
Run the program and press the buttons. The label made in BadWindow appears in the root window.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  QUIZ GUI Form_When admin panel is open, main quiz form is getting freeze Uday 4 795 Aug-25-2023, 08:24 PM
Last Post: deanhystad
  Prevent a PDF from being edited using Python? PythonNPC 1 2,318 May-05-2020, 09:04 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020