How To Fetch The Entry Text On A Different Window?
Solution 1:
I hope this piece of code will able to help you figure out how to implement the pop up text dialog box. The way of this example work by implementing a button in the main window (root), when you click on it, the pop up dialog will appear MyDialog
class object, will be created, then it will use wait_window()
to wait until it is finish. You can see how the pop up dialog box is implemented, it is an simple TopLevel
widget, it is just like another frame you can think of, you pack your label, your Entry
field, and finally a button mySubmitButton
.
Look down to the send
function, it is pretty much just getting the entry using simple .get()
method from the Entry
. And then you will destory()
the window, and resume back the main window. You can do it as many time as you want.
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()
In the sample output I did it two times. Entered George
and Sam
(my friend) as username, and the username will be updated everytime you open up a new dialog box.
Output:
>>>================================ RESTART ================================>>>
Username: George
Username: Sam
Edit: The submit button seems to be buggy? It sometimes doesn't want to be appear. It is clickable, but it does not appear until it is clicked. It appears to be only problem for Mac OS.
Reference used: effbot, stackoverflow, tutorialspoint, daniweb
Post a Comment for "How To Fetch The Entry Text On A Different Window?"