Home > Back-end >  Python: Tkinter open custom widget in new window
Python: Tkinter open custom widget in new window

Time:02-17

My aim is to generate a window with a button "NewWindow" on it. If I press the button, the program should open a new window. This window I stored in a class "NewWindow" to quickly reproduce it. In this "NewWindow" I have another button. If I press it the label of basic window should be updated and the window "NewWindow" should be closed automatically.

Here is my code:

from tkinter import *

class NewWindow(Toplevel):
    def __init__(self, master = None):
        super().__init__(master = master)
        self.title('NewWindow')

        self.lb = Label(self, text='Hello')
        self.lb.grid(column=0, row=0, columnspan=1)

        self.bt1 = Button(self, text="apply Hello", command= self.bt_press)
        self.bt1.grid(column=0, row=1)
    def bt_press(self):
        window.basic_lb.text = "Hello"

window = Tk()

def new_Editor():
    a = NewWindow(window)

window.title("BasicWindow")
window.basic_lb = Label(window, text='None')
window.basic_lb.grid(column=0, row=0, columnspan=1)

window.basic_bt = Button(window, text="NewWindow", command=new_Editor)
window.basic_bt.grid(column=0, row=1)


window.mainloop()

Problems:

  1. At start both windows NewWindow and BasicWindow are displayd. I only want to open BasicWindow and NewWindow should be opened after button basic_bt is clicked. How can I solve it? (already solved by commed below)
  2. Why the label text in basic_lb did not get some update after pressing self.bt1?
  3. How is it possible to close NewWindow with use of bt_press method?

CodePudding user response:

You have a few typos/errors in your code that are casuing some of your problems. As @Tim said, when you pass a function to a command like command=function(), it will be called on runtime, not when the button is pressed. You need to pass the function handle to the command, command=function. You got around this by using a lambda function in your button command, but it is easier to just have command=self.bt_press

Answering your second question, window.basic_lb.text = "Hello" is not how you change the text in a tkinter Label, use <Label>.config(text="Hello"). You also should use self.master and define self.master = master in __init__ instead of just using window, because while you can access window due to it not being defined in local scope, it's better to explicitly define it.

You can close a window using window.destroy().

Your working code is now:

from tkinter import *

class NewWindow(Toplevel):
    def __init__(self, master = None):
        super().__init__(master = master)
        self.title('NewWindow')
        self.master = master

        self.lb = Label(self, text='Hello')
        self.lb.grid(column=0, row=0, columnspan=1)

        self.bt1 = Button(self, text="apply Hello", command=self.bt_press)
        self.bt1.grid(column=0, row=1)
    def bt_press(self):
        self.master.basic_lb.config(text="Hello")
        self.destroy()

window = Tk()

def new_Editor():
    a = NewWindow(window)

window.title("BasicWindow")
window.basic_lb = Label(window, text='None')
window.basic_lb.grid(column=0, row=0, columnspan=1)

window.basic_bt = Button(window, text="NewWindow", command=new_Editor)
window.basic_bt.grid(column=0, row=1)

window.mainloop()
  • Related