Creating a popup message box with an Entry field
Asked Answered
K

4

23

I want to create a popup message box which prompts user to enter an input. I have this method inside a class. I am basing my code on this guide by java2s.

class MyDialog:
    def __init__(self, parent):
        top = self.top = Toplevel(parent)

        Label(top, text="Value").pack()

        self.e = Entry(top)
        self.e.pack(padx=5)

        b = Button(top, text="OK", command=self.ok)
        b.pack(pady=5)

    def ok(self):
        print "value is", self.e.get()
        self.top.destroy()

root = Tk()
d = MyDialog(root)

root.wait_window(d.top)

But in this, top = self.top = Toplevel(parent) doesn't work for me.

I have a mockup of what I am trying to accomplish.

GUI mockup

My program structure looks something like this:

class MainUI:
   def__int__(self):
       ...
       self.initUI()

   def initUI(self):
       .......
       Popup = Button(self, text="Enter Value", command=self.showPopup)

   def showPopup(self):
       #create the popup with an Entry here

How can I create a message box in Python which accepts user input?

Klan answered 4/4, 2012 at 23:51 Comment(2)
When you say "send entered data back to main form" what do you mean? Is this data going to be displayed in a widget?Hightest
By sending data back, I mean the value is that text box is stored in a variable which I manipulate later in the program.Klan
C
27

I'm a little confused about your two different blocks of code. Just addressing the first block of code, nothing happens because you never enter the mainloop. To do that, you need to call root.mainloop(). The typical way of doing this is to add a button to root widget and bind a callback function to the Button (which includes d=MyDialog() and root.wait_window(d.top))

Here's some basic code which I hope does what you want ...

from Tkinter import *
import sys

class popupWindow(object):
    def __init__(self,master):
        top=self.top=Toplevel(master)
        self.l=Label(top,text="Hello World")
        self.l.pack()
        self.e=Entry(top)
        self.e.pack()
        self.b=Button(top,text='Ok',command=self.cleanup)
        self.b.pack()
    def cleanup(self):
        self.value=self.e.get()
        self.top.destroy()

class mainWindow(object):
    def __init__(self,master):
        self.master=master
        self.b=Button(master,text="click me!",command=self.popup)
        self.b.pack()
        self.b2=Button(master,text="print value",command=lambda: sys.stdout.write(self.entryValue()+'\n'))
        self.b2.pack()

    def popup(self):
        self.w=popupWindow(self.master)
        self.b["state"] = "disabled" 
        self.master.wait_window(self.w.top)
        self.b["state"] = "normal"

    def entryValue(self):
        return self.w.value


if __name__ == "__main__":
    root=Tk()
    m=mainWindow(root)
    root.mainloop()

I get the value from the popupWindow and use it in the main program (take a look at the lambda function associated with b2).

Main window:

Main window

"Click me" window:

click me window

Main window while "click me" is open:

Main window with greyed out "click me"

Carbine answered 5/4, 2012 at 0:41 Comment(3)
I tried your code. Now how do I pass the value entered in that Entry in the form which popped up back to the mainWindow class? Maybe I have method def getVal(self): return self.e on the popupWindow class How do I call this from the mainWindow class ?Klan
@mgilson, this is what I exactly wanted to do. I got a bit confused on how to get the value from the other class. I was thinking in terms of Java and I assumed the class variables were private(the variable 'value' in popupWindow class). Now I understand.. The method entryValue() does what I wanted to do. Thank you!Klan
Is there any reason to use top=self.top=Toplevel(master)over self.top=Toplevel(master)?Wampumpeag
E
31
import tkinter as tk
from tkinter import simpledialog

ROOT = tk.Tk()

ROOT.withdraw()
# the input dialog
USER_INP = simpledialog.askstring(title="Test",
                                  prompt="What's your Name?:")

# check it out
print("Hello", USER_INP)

Enjoy ...

Estriol answered 1/7, 2020 at 8:13 Comment(3)
This question was answered eight years ago. What is the difference between your approach and the accepted answer? What benefit does it provide?Trichinopoly
@JeremyCaney it provides the obvious and significant benefit of being much more concise, and being completely devoid of any need for OOP boilerplate.Fifth
Should be accepted answer, also check this out: https://mcmap.net/q/584481/-tkinter-askstring-deleted-before-its-visibility-changedTew
C
27

I'm a little confused about your two different blocks of code. Just addressing the first block of code, nothing happens because you never enter the mainloop. To do that, you need to call root.mainloop(). The typical way of doing this is to add a button to root widget and bind a callback function to the Button (which includes d=MyDialog() and root.wait_window(d.top))

Here's some basic code which I hope does what you want ...

from Tkinter import *
import sys

class popupWindow(object):
    def __init__(self,master):
        top=self.top=Toplevel(master)
        self.l=Label(top,text="Hello World")
        self.l.pack()
        self.e=Entry(top)
        self.e.pack()
        self.b=Button(top,text='Ok',command=self.cleanup)
        self.b.pack()
    def cleanup(self):
        self.value=self.e.get()
        self.top.destroy()

class mainWindow(object):
    def __init__(self,master):
        self.master=master
        self.b=Button(master,text="click me!",command=self.popup)
        self.b.pack()
        self.b2=Button(master,text="print value",command=lambda: sys.stdout.write(self.entryValue()+'\n'))
        self.b2.pack()

    def popup(self):
        self.w=popupWindow(self.master)
        self.b["state"] = "disabled" 
        self.master.wait_window(self.w.top)
        self.b["state"] = "normal"

    def entryValue(self):
        return self.w.value


if __name__ == "__main__":
    root=Tk()
    m=mainWindow(root)
    root.mainloop()

I get the value from the popupWindow and use it in the main program (take a look at the lambda function associated with b2).

Main window:

Main window

"Click me" window:

click me window

Main window while "click me" is open:

Main window with greyed out "click me"

Carbine answered 5/4, 2012 at 0:41 Comment(3)
I tried your code. Now how do I pass the value entered in that Entry in the form which popped up back to the mainWindow class? Maybe I have method def getVal(self): return self.e on the popupWindow class How do I call this from the mainWindow class ?Klan
@mgilson, this is what I exactly wanted to do. I got a bit confused on how to get the value from the other class. I was thinking in terms of Java and I assumed the class variables were private(the variable 'value' in popupWindow class). Now I understand.. The method entryValue() does what I wanted to do. Thank you!Klan
Is there any reason to use top=self.top=Toplevel(master)over self.top=Toplevel(master)?Wampumpeag
T
3

I did it in Tkinter without any classes. I created a function that starts a new window.

popup.Tk()
popup.mainloop()

In that window there is an Entry field from where I get the text with a variable which value is: entry.get()

Then you can use that variable for whatever you need and it will take the text from that Entry field.

Trajectory answered 10/9, 2021 at 2:2 Comment(0)
F
-1

I just tried this:

def get_me(): s = simpledialog.askstring("input string", "please input your added text")

Source: https://www.youtube.com/watch?v=43vzP1FyAF8

Federation answered 13/7, 2020 at 2:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.