【问题标题】:User input in dialog box对话框中的用户输入
【发布时间】:2018-10-30 11:32:16
【问题描述】:

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

a = input('Enter your string here:') 

我想要一个对话框来代替这个,以便用户可以在那里输入。

这没有达到目的。这仅显示对话框,您不能提供输入条目。

import ctypes  # An included library with Python install.   
ctypes.windll.user32.MessageBoxW(0, "Your text", "Your title", 1)

【问题讨论】:

  • 没关系。 :) 我想避免使用外部库。
  • @iCodez 和 RishuA。当您删除对话的第一部分而不是其余部分时,其他人会感到很困惑。
  • 啊!似乎在编辑时被删除了。来自手机。
  • 这就是我正在使用的。 root = tk.Tk() root.withdraw() in_csv_file = simpledialog.askstring("Some_Name", "Enter CSV file Name",parent=root)
  • RishuA:要格式化 cmets 中的代码,请使用反引号(`)字符,将代码括在其中。像这样:root = tk.Tk()root.withdraw()in_csv_file = simpledialog.askstring("Some_Name", "Enter CSV file Name",parent=root)

标签: python python-3.x user-interface tkinter


【解决方案1】:

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

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

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

谷歌“python easygui”了解更多详细信息。
你可以从pypi获取easygui。

【讨论】:

  • 问题是我想避免使用外部库,由于我的环境中的某些正当原因,只想使用可用于 python 安装。
  • easygui 是基于 tcl 有点误导。在内部,它使用tkinter,这是一个标准的 Python 库,用于连接到 tk/tcl。 @RishuAl:即使你不能使用第三方库,下载easygui并查看它的源代码(即enterbox()函数是如何实现的)可能对你很有用。跨度>
  • @martineau 谢谢先生。一定会看看的。
【解决方案2】:

这是我不久前创建的一个模块,用于使用 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()

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

【讨论】:

  • 感谢您的努力。但相反,我可以使用 root= tk.Tk() rootwithdraw() in = simpledialog.askstring("Input", "blba bla" Name",parent=root)
  • 如前所述,由于某些原因,我不想使用任何外部库。
  • @RishuA 只需将其复制到您的代码中,然后将if __name__=='__main__':之后的所有内容都省略
  • 我明白你的意思。我对这个问题的意图是最短的解决方案(最短的代码)。检查我的问题。但是,我真的很感谢你为我写的努力。我一定会在需要时尝试一下。
  • @RishuA 我认为标准的 tk 三行代码是最好的,没有任何外部代码。 (三行输入或一行打印)
【解决方案3】:

我认为这是您在没有外部任何东西的情况下获得的最短时间:


开始:

from tkinter import *
root=Tk()

而不是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()

带函数DoSomethingWithInput(a)


而不是print('some text')

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

使用DoSomething() 作为您接下来要做的事情。

【讨论】:

  • 谢谢!我知道。 :) 你真的很努力,我尊重这一点。
  • RishuA:您可以使用tkinter 轻松创建自定义对话框。这是一些(过时的 Python 2)documentation,但如果您在线搜索(包括此网站),您可能会找到更多最新信息。
【解决方案4】:

使用乌龟。 turtle.textinput("title", "prompt")

这是一个例子:

from turtle import textinput

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

print("Hello", name + "!")

【讨论】:

    猜你喜欢
    • 2012-06-10
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多