I want to make a button that when clicked opens a new window (tk.toplevel), is there a way to make the button only work once, (it only makes one new window when clicked thereafter the the button when clicked does not do anything)
from the below code I can make unlimited amount of top levels by clicking the button BUT I just want it to create one on click.
import tkinter as tk
class run: # pls ignore if there are any syntrax error/spelling because I typed this in stack overflow
def __init___(self, master)
button = tk.button(master, text="btn", self.command=make_new)
button.grid()
def make_new()
new = tk.toplevel(master)
master1 = tk.Tk()
i = Run(master1)
master1.mainloop()
*** I didn't put the original code because it has too many classes/functions to put here
I made some changes to your code so that when you click the button, it will create a toplevel, and if you click multiple times, it will not create more of them:
import tkinter as tk
class Run:
def __init__(self, master):
self.master = master
self.toplevels = 0
button = tk.Button(master, text="btn", command=self.make_new)
button.pack()
def make_new(self):
if not self.toplevels:
new = tk.Toplevel(self.master)
self.toplevels += 1
master1 = tk.Tk()
i = Run(master1)
master1.mainloop()
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments