User input in dialog box

后端 未结 3 1709
栀梦
栀梦 2021-02-20 08:16

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

相关标签:
3条回答
  • 2021-02-20 08:24

    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.

    0 讨论(0)
  • 2021-02-20 08:24

    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.

    0 讨论(0)
  • 2021-02-20 08:41

    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.

    0 讨论(0)
提交回复
热议问题