how to make only 1 new window when a button is clicked? tkinter

coder_not_found

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

MercifulSory

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python tkinter: How can I ensure only ONE child window is created onclick and not a new window every time the button is clicked?

From Dev

Python tkinter: How can I ensure only ONE child window is created onclick and not a new window every time the button is clicked?

From Dev

how to make a link in a mysql result and open a new window when clicked?

From Dev

How to make only one Button selected when clicked -QUIZ ANDROID

From Dev

How can i make data submitted by user to be into a new Firebase key, key only created when Submit button is clicked to pass the data

From Dev

How can I make a button that is disabled for 1 minute when clicked?

From Dev

Tkinter - How can I make a button can create new window and close the main window

From Dev

Tkinter when a button is clicked?

From Dev

How to open a new activity when a button is clicked?

From Dev

How to make a Random Layout when button clicked

From Dev

how to display a div only when a button is clicked

From Dev

Addmefast.com checks the Like button clicked in new window? How

From Dev

In Python Tkinter, I have successfully made a keybind but it only works when I am clicked into the tkinter window.

From Dev

JFrame: How to hide main window when button is clicked?

From Dev

How to make state of window idle until button is pressed in python tkinter?

From Dev

How to make button command to open image inside window in tkinter?

From Dev

How do I make this button quit the parent window in tkinter?

From Dev

How to create a new label when a button is clicked in javafx and scenebuilder?

From Dev

How to get a table on a new web page when a button is Clicked

From Java

How to make a circular ripple on a button when it's being clicked?

From Dev

how to make items true in an object when user clicked on button vuejs?

From Dev

How to scroll to an element when a button is clicked and also make it hovered?

From Dev

How to make content navigation button (remains pressed when clicked) in Vaadin

From Dev

How to make a JFrame show advanced options when a button is clicked

From Dev

How to create a black curtain that only appears when a button is clicked?

From Dev

How to perform action for a menu item only when button is clicked

From Dev

Rails how to download only current record when clicked download button

From Dev

jQuery: How to enable button when only all check boxes are clicked?

From Dev

How can I change the color of a button with CSS only when it is clicked?

Related Related

  1. 1

    Python tkinter: How can I ensure only ONE child window is created onclick and not a new window every time the button is clicked?

  2. 2

    Python tkinter: How can I ensure only ONE child window is created onclick and not a new window every time the button is clicked?

  3. 3

    how to make a link in a mysql result and open a new window when clicked?

  4. 4

    How to make only one Button selected when clicked -QUIZ ANDROID

  5. 5

    How can i make data submitted by user to be into a new Firebase key, key only created when Submit button is clicked to pass the data

  6. 6

    How can I make a button that is disabled for 1 minute when clicked?

  7. 7

    Tkinter - How can I make a button can create new window and close the main window

  8. 8

    Tkinter when a button is clicked?

  9. 9

    How to open a new activity when a button is clicked?

  10. 10

    How to make a Random Layout when button clicked

  11. 11

    how to display a div only when a button is clicked

  12. 12

    Addmefast.com checks the Like button clicked in new window? How

  13. 13

    In Python Tkinter, I have successfully made a keybind but it only works when I am clicked into the tkinter window.

  14. 14

    JFrame: How to hide main window when button is clicked?

  15. 15

    How to make state of window idle until button is pressed in python tkinter?

  16. 16

    How to make button command to open image inside window in tkinter?

  17. 17

    How do I make this button quit the parent window in tkinter?

  18. 18

    How to create a new label when a button is clicked in javafx and scenebuilder?

  19. 19

    How to get a table on a new web page when a button is Clicked

  20. 20

    How to make a circular ripple on a button when it's being clicked?

  21. 21

    how to make items true in an object when user clicked on button vuejs?

  22. 22

    How to scroll to an element when a button is clicked and also make it hovered?

  23. 23

    How to make content navigation button (remains pressed when clicked) in Vaadin

  24. 24

    How to make a JFrame show advanced options when a button is clicked

  25. 25

    How to create a black curtain that only appears when a button is clicked?

  26. 26

    How to perform action for a menu item only when button is clicked

  27. 27

    Rails how to download only current record when clicked download button

  28. 28

    jQuery: How to enable button when only all check boxes are clicked?

  29. 29

    How can I change the color of a button with CSS only when it is clicked?

HotTag

Archive