19

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)
Artemis
  • 2,553
  • 7
  • 21
  • 36
RonyA
  • 585
  • 3
  • 11
  • 26
  • Its Ok . :) I want to avoid external libraries . – RonyA May 20 '18 at 19:01
  • 1
    @iCodez and RishuA. It is rather confusing for others when you delete the first part of a conversation but not the rest. – Artemis May 21 '18 at 17:39
  • Ah ! Seems while editing got deleted . Was from cell phone. – RonyA May 21 '18 at 18:05
  • 1
    This is what I am using. root = tk.Tk() root.withdraw() in_csv_file = simpledialog.askstring("Some_Name", "Enter CSV file Name",parent=root) – RonyA May 22 '18 at 15:37
  • 1
    RishuA: To format code in comments, use the backtick(` ) character, enclosing the code in them. Like this: `root = tk.Tk()` `root.withdraw()` `in_csv_file = simpledialog.askstring("Some_Name", "Enter CSV file Name",parent=root)` – Artemis May 22 '18 at 18:48
  • Thanks. I was not aware of this .Will edit . – RonyA May 22 '18 at 18:49
  • Somehow I am not able to to get the edit option. – RonyA May 22 '18 at 18:52
  • RishuA: You have 2mins to edit a comment. Delete, repost, then delete this thread of comments. – Artemis May 22 '18 at 18:53
  • Got it. Will keep that in mind. – RonyA May 22 '18 at 18:54

5 Answers5

13

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • The 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 May 20 '18 at 18:54
  • Saying `easygui` is based on tcl is a slightly misleading. Internally it uses `tkinter` 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 download `easygui` and look at its source code (i.e. how the `enterbox()` function is implemented). – martineau May 20 '18 at 19:32
  • 1
    @martineau Thank You sir . Will definitely have a look to that. – RonyA May 20 '18 at 19:45
8

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.

Artemis
  • 2,553
  • 7
  • 21
  • 36
  • Thank you ! Tk am aware. :) You are really putting efforts and I respect that . – RonyA May 20 '18 at 19:44
  • RishuA: You can easily create custom dialog boxes with `tkinter`. Here's some (dated Python 2) [documentation](http://effbot.org/tkinterbook/tkinter-dialog-windows.htm), but it you search online (including this website) you'll probably be able to locate more current information. – martineau May 20 '18 at 21:34
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.

Artemis
  • 2,553
  • 7
  • 21
  • 36
  • Thanks for your efforts. But Instead i can use root= tk.Tk() rootwithdraw() in = simpledialog.askstring("Input", "blba bla" Name",parent=root) – RonyA May 20 '18 at 19:20
  • As mentioned I do not want to use any external libraries due to some reasons. – RonyA May 20 '18 at 19:22
  • @RishuA Just copy it into your code, and leave off everything after `if __name__=='__main__':` – Artemis May 20 '18 at 19:23
  • I 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. – RonyA May 20 '18 at 19:25
  • @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) – Artemis May 20 '18 at 19:31
  • Yeah, as of now it seems so. – RonyA May 20 '18 at 19:32
2

Use turtle. turtle.textinput("title", "prompt")

Here is an example:

from turtle import textinput

name = textinput("Name", "Please enter your name:")

print("Hello", name + "!")
0
from tkinter import simpledialog
item = simpledialog.askstring("New Item", "Enter name of item:")
print(item)

https://docs.python.org/3/library/dialog.html Not many lines needed for tkinter :)

catchpolej
  • 21
  • 7
  • 3
    Trying this, I get a Runtime Error: "Too early to create dialog window: no default root window" – spoko Mar 02 '23 at 19:52