link
link

Reputation: 23

How to run a function as a button command in tkinter from a 2nd window

Hi I am pretty new to tkinter and have being trying to create a button that opens a window then a have a button in the new window the gives a message when pressed. I ran into the problem that the only whay I could get it to recognise the function I wrote was to write it inside the function that opens the second window. I don't know if I have being searching for the wrong things but I can't find how to do this properly. Can someone help me out Here is my code

from tkinter import *
master = Tk()
master.title("frame control")


def win():
    window2 = Toplevel()

    def open():
        stamp = Label(window2, text="Staped").pack()
    lab2 = Button(window2,text = "yo ",command = open).pack()


lab1 = Button(master,text = " open a new window" , command = win).pack()


mainloop()

Upvotes: 0

Views: 583

Answers (1)

TheLizzard
TheLizzard

Reputation: 7710

This is your code but with best practises:

import tkinter as tk

def create_stamp():
    stamp = tk.Label(window2, text="Stamp")
    stamp.pack()

def create_second_win():
    global window2
    window2 = tk.Toplevel(root)
    lab2 = tk.Button(window2, text="Click me", command=create_stamp)
    lab2.pack()


root = tk.Tk()
root.title("Frame control")

button = tk.Button(root, text="Open a new window", command=create_second_win)
button.pack()

root.mainloop()

I made window2 a global variable so that I can access it from create_stamp. Generally it is discouraged to use from ... import *. As @Matiiss said, sometimes you can have problems with global variables if you don't keep track of the variable names that you used.

If you want to avoid using global variables and want to use classes, look at this:

import tkinter as tk


class App:
    def __init__(self):
        self.stamps = []

        self.root = tk.Tk()
        self.root.title("Frame control")

        self.button = tk.Button(self.root, text="Open a new window", command=self.create_second_win)
        self.button.pack()

    def create_stamp(self):
        stamp = tk.Label(self.window2, text="Stamp")
        stamp.pack()
        self.stamps.append(stamp)

    def create_second_win(self):
        self.window2 = tk.Toplevel(self.root)
        self.lab2 = tk.Button(self.window2, text="Click me", command=self.create_stamp)
        self.lab2.pack()

    def mainloop(self):
        self.root.mainloop()


if __name__ == "__main__":
    app = App()
    app.mainloop()

As @Matiiss mentioned it would be more organised if you move the second window to its own class. For bigger projects it is a must but in this case you don't have to.

Upvotes: 1

Related Questions