Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrect way to implement a custom popup tkinter dialog box
    text
    copied!<p>I just started learning how to create a custom pop up dialog box; and as it turns out, the <code>tkinter messagebox</code> is really easy to use, but it also does not do too much. Here is my attempt to create a dialog box that will take input and then store that in the username.</p> <p>My question is what is the recommended style to implement this? As Bryan Oakley suggested in <a href="https://stackoverflow.com/questions/10052410/tkinter-button-does-not-appear-on-toplevel#comment12867705_10052410">this comment</a>. </p> <blockquote> <p>I would advise against using a global variable. Instead of having the dialog destroy itself, have it destroy only the actual widget but leave the object alive. Then, call something like <code>inputDialog.get_string()</code> and then <code>del inputDialog</code> from your main logic. </p> </blockquote> <p>Maybe using the global variable to return my string is not the best idea, but why? And what is the suggested way? I get confused because I don't know how to trigger the getstring once the window is destroyed, and... the line about destroying the actual widget, I am not sure if he is referring to <code>TopLevel</code>.</p> <p>The reason I ask is because I want the pop up box to be destroyed after I press the submit button; because after all, I want it to resume back to the main program, update something, etc. What should the button method <code>send</code> do in this case? Because the idea in this particular example is to allow the user to do it over and over, if he desires.</p> <pre><code>import tkinter as tk class MyDialog: def __init__(self, parent): top = self.top = tk.Toplevel(parent) self.myLabel = tk.Label(top, text='Enter your username below') self.myLabel.pack() self.myEntryBox = tk.Entry(top) self.myEntryBox.pack() self.mySubmitButton = tk.Button(top, text='Submit', command=self.send) self.mySubmitButton.pack() def send(self): global username username = self.myEntryBox.get() self.top.destroy() def onClick(): inputDialog = MyDialog(root) root.wait_window(inputDialog.top) print('Username: ', username) username = 'Empty' root = tk.Tk() mainLabel = tk.Label(root, text='Example for pop up input box') mainLabel.pack() mainButton = tk.Button(root, text='Click me', command=onClick) mainButton.pack() root.mainloop() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload