Python 用户在对话框中输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50438182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:29:55  来源:igfitidea点击:

User input in dialog box

pythonpython-3.xuser-interfacetkinter

提问by RonyA

Is there any library available in python for the graphical user entry input. I know about tkbut I believe it takes some line of codes to do that. I am looking for the shortest solution.

python 中是否有任何库可用于图形用户输入输入。我知道,tk但我相信需要一些代码才能做到这一点。我正在寻找最短的解决方案。

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)

回答by user3133732 Ver. 2.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.

您有两种解决方案选择。你可以通过pip获取两个包,一个是easygui,另一个是easygui_qt。easygui 是基于 tcl 的,easygui_qt 是基于 qt 窗口管理器的,设置起来有点困难,但使用起来也一样简单,还有更多的选项。

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...

他们需要使用的只是导入包,import easygui然后,要获得用户响应,您将使用一行...

myvar = easygui.enterbox("What, is your favorite color?")

Google "python easygui" for more detailed info.
You can get easygui from pypi.

谷歌“python easygui”以获得更详细的信息。
您可以从pypi获取 easygui 。

回答by Artemis still doesn't trust SE

Here is a module I created a while ago to manage basic printing and input with GUI. It uses tkinter:

这是我不久前创建的一个模块,用于使用 GUI 管理基本打印和输入。它使用 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.

它包括如何使用它的示例。

回答by Artemis still doesn't trust SE

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=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)

带功能 DoSomethingWithInput(a)



Instead of print('some text'):

而不是print('some text')

Label(root, text='some text').pack()
Button(root, text='Ok', command=DoSomething).pack()

With DoSomething()as what you do next.

随着DoSomething()你下一步做什么。