Why does my tkinter window only work when created globably?











up vote
0
down vote

favorite












When I created this module I first made the tkinter window (all of its settings globally) it worked as intended. I could run the module and the window worked, taking the input from the entry field and displaying the welcome or error message. But when I put them into a function, it stopped working correctly, as shown.
How the window looks when created globally, with the button and input working:



https://gyazo.com/ffcb16416b8a971c09bfa60ee9367bbd



How it looks when created inside the function:



https://gyazo.com/c8858a2793befafa41e71d1099f021d3



The error message pops up straight away, then the main window with the entry field but not the button.



Here's the code where I created the window and settings inside a function:



def userSign(userEntry):
userId = userEntry.get()
if userId.isdigit() == True and len(userId) == 4:
welcomeWindow = tkinter.Tk()
welcomeWindow.title("Welcome")
welcomeWindow.geometry("200x50")
welcome = tkinter.Label(master=welcomeWindow, text="Welcome "+userId,font=("Helvetica", 18, "bold"))
welcome.grid()
welcomeWindow.mainloop()
else:
errorWindow = tkinter.Tk()
errorWindow.title("ERROR")
errorWindow.geometry("500x50")
error = tkinter.Label(master=errorWindow, text="ERROR: "+userId +" DOES NOT MEET CRITERIA", font=("Helvetica", 18, "bold"))
error.grid()
userId=""
errorWindow.mainloop()

def show():
window = tkinter.Tk()
window.title("Sign In")
window.geometry("250x100")

signInPrompt = tkinter.Label(master = window, text = "Enter your ID to sign in")
signInPrompt.grid(column=0,row=2)

userEntry = tkinter.Entry(master = window)
userEntry.grid(column=0,row=4)

enterButton = tkinter.Button(master = window, text="Sign in", command=userSign(userEntry))
enterButton.grid(column=0,row=6)

window.mainloop()


How do I get it so that my window works correctly when created inside functions as this module needs to be called by a different, main module.










share|improve this question


















  • 1




    Possible duplicate of Why is Button parameter “command” executed when declared?
    – fhdrsdg
    Nov 22 at 14:13















up vote
0
down vote

favorite












When I created this module I first made the tkinter window (all of its settings globally) it worked as intended. I could run the module and the window worked, taking the input from the entry field and displaying the welcome or error message. But when I put them into a function, it stopped working correctly, as shown.
How the window looks when created globally, with the button and input working:



https://gyazo.com/ffcb16416b8a971c09bfa60ee9367bbd



How it looks when created inside the function:



https://gyazo.com/c8858a2793befafa41e71d1099f021d3



The error message pops up straight away, then the main window with the entry field but not the button.



Here's the code where I created the window and settings inside a function:



def userSign(userEntry):
userId = userEntry.get()
if userId.isdigit() == True and len(userId) == 4:
welcomeWindow = tkinter.Tk()
welcomeWindow.title("Welcome")
welcomeWindow.geometry("200x50")
welcome = tkinter.Label(master=welcomeWindow, text="Welcome "+userId,font=("Helvetica", 18, "bold"))
welcome.grid()
welcomeWindow.mainloop()
else:
errorWindow = tkinter.Tk()
errorWindow.title("ERROR")
errorWindow.geometry("500x50")
error = tkinter.Label(master=errorWindow, text="ERROR: "+userId +" DOES NOT MEET CRITERIA", font=("Helvetica", 18, "bold"))
error.grid()
userId=""
errorWindow.mainloop()

def show():
window = tkinter.Tk()
window.title("Sign In")
window.geometry("250x100")

signInPrompt = tkinter.Label(master = window, text = "Enter your ID to sign in")
signInPrompt.grid(column=0,row=2)

userEntry = tkinter.Entry(master = window)
userEntry.grid(column=0,row=4)

enterButton = tkinter.Button(master = window, text="Sign in", command=userSign(userEntry))
enterButton.grid(column=0,row=6)

window.mainloop()


How do I get it so that my window works correctly when created inside functions as this module needs to be called by a different, main module.










share|improve this question


















  • 1




    Possible duplicate of Why is Button parameter “command” executed when declared?
    – fhdrsdg
    Nov 22 at 14:13













up vote
0
down vote

favorite









up vote
0
down vote

favorite











When I created this module I first made the tkinter window (all of its settings globally) it worked as intended. I could run the module and the window worked, taking the input from the entry field and displaying the welcome or error message. But when I put them into a function, it stopped working correctly, as shown.
How the window looks when created globally, with the button and input working:



https://gyazo.com/ffcb16416b8a971c09bfa60ee9367bbd



How it looks when created inside the function:



https://gyazo.com/c8858a2793befafa41e71d1099f021d3



The error message pops up straight away, then the main window with the entry field but not the button.



Here's the code where I created the window and settings inside a function:



def userSign(userEntry):
userId = userEntry.get()
if userId.isdigit() == True and len(userId) == 4:
welcomeWindow = tkinter.Tk()
welcomeWindow.title("Welcome")
welcomeWindow.geometry("200x50")
welcome = tkinter.Label(master=welcomeWindow, text="Welcome "+userId,font=("Helvetica", 18, "bold"))
welcome.grid()
welcomeWindow.mainloop()
else:
errorWindow = tkinter.Tk()
errorWindow.title("ERROR")
errorWindow.geometry("500x50")
error = tkinter.Label(master=errorWindow, text="ERROR: "+userId +" DOES NOT MEET CRITERIA", font=("Helvetica", 18, "bold"))
error.grid()
userId=""
errorWindow.mainloop()

def show():
window = tkinter.Tk()
window.title("Sign In")
window.geometry("250x100")

signInPrompt = tkinter.Label(master = window, text = "Enter your ID to sign in")
signInPrompt.grid(column=0,row=2)

userEntry = tkinter.Entry(master = window)
userEntry.grid(column=0,row=4)

enterButton = tkinter.Button(master = window, text="Sign in", command=userSign(userEntry))
enterButton.grid(column=0,row=6)

window.mainloop()


How do I get it so that my window works correctly when created inside functions as this module needs to be called by a different, main module.










share|improve this question













When I created this module I first made the tkinter window (all of its settings globally) it worked as intended. I could run the module and the window worked, taking the input from the entry field and displaying the welcome or error message. But when I put them into a function, it stopped working correctly, as shown.
How the window looks when created globally, with the button and input working:



https://gyazo.com/ffcb16416b8a971c09bfa60ee9367bbd



How it looks when created inside the function:



https://gyazo.com/c8858a2793befafa41e71d1099f021d3



The error message pops up straight away, then the main window with the entry field but not the button.



Here's the code where I created the window and settings inside a function:



def userSign(userEntry):
userId = userEntry.get()
if userId.isdigit() == True and len(userId) == 4:
welcomeWindow = tkinter.Tk()
welcomeWindow.title("Welcome")
welcomeWindow.geometry("200x50")
welcome = tkinter.Label(master=welcomeWindow, text="Welcome "+userId,font=("Helvetica", 18, "bold"))
welcome.grid()
welcomeWindow.mainloop()
else:
errorWindow = tkinter.Tk()
errorWindow.title("ERROR")
errorWindow.geometry("500x50")
error = tkinter.Label(master=errorWindow, text="ERROR: "+userId +" DOES NOT MEET CRITERIA", font=("Helvetica", 18, "bold"))
error.grid()
userId=""
errorWindow.mainloop()

def show():
window = tkinter.Tk()
window.title("Sign In")
window.geometry("250x100")

signInPrompt = tkinter.Label(master = window, text = "Enter your ID to sign in")
signInPrompt.grid(column=0,row=2)

userEntry = tkinter.Entry(master = window)
userEntry.grid(column=0,row=4)

enterButton = tkinter.Button(master = window, text="Sign in", command=userSign(userEntry))
enterButton.grid(column=0,row=6)

window.mainloop()


How do I get it so that my window works correctly when created inside functions as this module needs to be called by a different, main module.







python tkinter






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 13:52









k.apps1999

33




33








  • 1




    Possible duplicate of Why is Button parameter “command” executed when declared?
    – fhdrsdg
    Nov 22 at 14:13














  • 1




    Possible duplicate of Why is Button parameter “command” executed when declared?
    – fhdrsdg
    Nov 22 at 14:13








1




1




Possible duplicate of Why is Button parameter “command” executed when declared?
– fhdrsdg
Nov 22 at 14:13




Possible duplicate of Why is Button parameter “command” executed when declared?
– fhdrsdg
Nov 22 at 14:13












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










You are creating two instances of Tk() which is a bad idea. Instead use Toplevel() for additional windows.



When you create variables or widgets inside a function the names are in the local scope and not available outside the function. And whan the function ends they will be garbage colletced.



Also, as @fhdrsdg points out, problems in the button command.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432491%2fwhy-does-my-tkinter-window-only-work-when-created-globably%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    You are creating two instances of Tk() which is a bad idea. Instead use Toplevel() for additional windows.



    When you create variables or widgets inside a function the names are in the local scope and not available outside the function. And whan the function ends they will be garbage colletced.



    Also, as @fhdrsdg points out, problems in the button command.






    share|improve this answer

























      up vote
      0
      down vote



      accepted










      You are creating two instances of Tk() which is a bad idea. Instead use Toplevel() for additional windows.



      When you create variables or widgets inside a function the names are in the local scope and not available outside the function. And whan the function ends they will be garbage colletced.



      Also, as @fhdrsdg points out, problems in the button command.






      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        You are creating two instances of Tk() which is a bad idea. Instead use Toplevel() for additional windows.



        When you create variables or widgets inside a function the names are in the local scope and not available outside the function. And whan the function ends they will be garbage colletced.



        Also, as @fhdrsdg points out, problems in the button command.






        share|improve this answer












        You are creating two instances of Tk() which is a bad idea. Instead use Toplevel() for additional windows.



        When you create variables or widgets inside a function the names are in the local scope and not available outside the function. And whan the function ends they will be garbage colletced.



        Also, as @fhdrsdg points out, problems in the button command.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 14:15









        figbeam

        2,580137




        2,580137






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432491%2fwhy-does-my-tkinter-window-only-work-when-created-globably%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)