User input in dialog box
Solution 1
You have two choices for a solution. There are two packages you can pip to get, one is easygui, the other is easygui_qt. easygui is based on tcl, and easygui_qt is based on the qt Window manager and is a little more difficult to set up, but just as simple to use, with a few more options.
All they require to use is to import the package, import easygui
, and after that, to get a user response you would use one line...
myvar = easygui.enterbox("What, is your favorite color?")
Google "python easygui" for more detailed info.
You can get easygui from pypi.
Solution 2
I think this is the shortest you'll get without anything external:
To start:
from tkinter import *
root=Tk()
Instead of a=input('enter something')
:
a=StringVar()
Label(root, text='enter something').pack()
Entry(root, textvariable=a).pack()
Button(root, text='Ok', command=lambda:DoSomethingWithInput(a.get)).pack()
With a function DoSomethingWithInput(a)
Instead of print('some text')
:
Label(root, text='some text').pack()
Button(root, text='Ok', command=DoSomething).pack()
With DoSomething()
as what you do next.
Solution 3
Here is a module I created a while ago to manage basic printing and input with GUI. It uses tkinter:
from tkinter import *
def donothing(var=''):
pass
class Interface(Tk):
def __init__(self, name='Interface', size=None):
super(interface, self).__init__()
if size:
self.geometry(size)
self.title(name)
self.frame = Frame(self)
self.frame.pack()
def gui_print(self, text='This is some text', command=donothing):
self.frame.destroy()
self.frame = Frame(self)
self.frame.pack()
Label(self.frame, text=text).pack()
Button(self.frame, text='Ok', command=command).pack()
def gui_input(self, text='Enter something', command=donothing):
self.frame.destroy()
self.frame = Frame(self)
self.frame.pack()
Label(self.frame, text=text).pack()
entry = StringVar(self)
Entry(self.frame, textvariable=entry).pack()
Button(self.frame, text='Ok', command=lambda: command(entry.get())).pack()
def end(self):
self.destroy()
def start(self):
mainloop()
# -- Testing Stuff --
def foo(value):
global main
main.gui_print(f'Your name is {value}.', main.end)
def bar():
global main
main.gui_input('What is your name?', foo)
if __name__ == '__main__':
main = interface('Window')
bar()
main.start()
It includes an example of how to use it.
Solution 4
Use turtle. turtle.textinput("title", "prompt")
Here is an example:
from turtle import textinput
name = textinput("Name", "Please enter your name:")
print("Hello", name + "!")
RonyA
Updated on June 07, 2021Comments
-
RonyA almost 3 years
Is there any library available in python for the graphical user entry input. I know about
tk
but I believe it takes some line of codes to do that. I am looking for the shortest solution.a = input('Enter your string here:')
In place of this, I want to get a dialogue box so that user can input there.
This did not serve the purpose. This only shows the dialogue box and you can't provide an input entry.
import ctypes # An included library with Python install. ctypes.windll.user32.MessageBoxW(0, "Your text", "Your title", 1)
-
RonyA almost 6 yearsThe problem is that I want to avoid the external libraries , only want to use available with python install due to some valid reason in my environment.
-
RonyA almost 6 yearsThanks for your efforts. But Instead i can use root= tk.Tk() rootwithdraw() in = simpledialog.askstring("Input", "blba bla" Name",parent=root)
-
RonyA almost 6 yearsAs mentioned I do not want to use any external libraries due to some reasons.
-
Artemis almost 6 years@RishuA Just copy it into your code, and leave off everything after
if __name__=='__main__':
-
RonyA almost 6 yearsI understood your point . My intention for this question was for the shortest solution(shortest code) . Check my Question. However, i really appreciate your effort to write it for me. i'll definetly try it when needed.
-
Artemis almost 6 years@RishuA I think the standard tk three lines of code is the best you'll get without anything external. (three lines for input or one for print)
-
RonyA almost 6 yearsYeah, as of now it seems so.
-
martineau almost 6 yearsSaying
easygui
is based on tcl is a slightly misleading. Internally it usestkinter
which is a standard Python library for interfacing to tk/tcl. @RishuAl: Even if you can't use a third-party library, it would probably be very useful for you to do downloadeasygui
and look at its source code (i.e. how theenterbox()
function is implemented). -
RonyA almost 6 yearsThank you ! Tk am aware. :) You are really putting efforts and I respect that .
-
RonyA almost 6 years@martineau Thank You sir . Will definitely have a look to that.
-
martineau almost 6 yearsRishuA: You can easily create custom dialog boxes with
tkinter
. Here's some (dated Python 2) documentation, but it you search online (including this website) you'll probably be able to locate more current information.