Python:获取一个复选框——最简单的方法

标签 python user-interface tkinter

或者也许是懒惰的方式..

我正在寻找一个 python 模块,它有一些内置的 GUI 方法来获得快速的用户输入——一个非常常见的编程案例。必须在 Windows 7 上工作

我的理想情况

import magicGUImodule
listOfOptions = ["option 1", "option 2", "option 3"]
choosenOptions = magicGUImodule.getChecklist(listOfOptions, 
                            selectMultiple=True, cancelButton=True)

它有点像 raw_input 但带有 GUI。一定有什么东西在那里,因为这是一个常见的编程任务。


更新

@alecxe 我取消选中您的答案作为我的问题的解决方案并不是无礼的。我仍然希望能够在我正在处理的任何脚本中使用我的理想案例,而您的回答让我成功了一半。

我认为我可以很容易地将@alecxe 的解决方案实现到一个模块中,但这并不那么简单(对我来说)..

到目前为止,这是我的模块:

# This serve as a module to get user input - the easy way!
# Some GUI selection
#from Tkinter import *
import Tkinter

master = Tkinter.Tk()
input = None
listbox = None

def chooseFromList(list, windowTitle="Choose from list", buttonText="Submit", selectMultiple=False, w=150, h=30):
    global listbox
    listbox = Tkinter.Listbox(master, selectmode=MULTIPLE if selectMultiple else SINGLE, width=w, height=h)
    listbox.master.title(windowTitle)
    for option in list:
        listbox.insert(0, option)
    listbox.pack()
    #listbox.selection_set(1)
    b = Tkinter.Button(master, command=callback(listbox), text=buttonText)
    b.pack()
    mainloop()

def callback(listbox):
    global listbox
    setInput(listbox.selection_get())
    master.destroy()    

def setInput(var):
    global input
    input = var

def getInput():
    global input
    return input

这是我的脚本

import GetUserInput
listOfOptions = ["option 1", "option 2", "option 3"]
choice = GetUserInput.chooseFromList(listOfOptions)
print choice.getInput()

但我只是得到错误

无法调用“listbox”命令:应用程序已被销毁

已经尝试了很多不同的选项,我认为这些选项可以解决这个问题(比如使用全局变量)——但没有任何运气。

更新 2

@blablatros 给了我正在寻找的解决方案。

最佳答案

Easygui模块正是您所需要的:

import easygui as eg

question = "This is your question"
title = "This is your window title"
listOfOptions = ["option 1", "option 2", "option 3"]

choice = eg.multchoicebox(question , title, listOfOptions)

choice 将返回所选答案的列表。

对多项选择题使用multchoicebox,对单选题使用choicebox

关于Python:获取一个复选框——最简单的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17185134/

相关文章:

Java,actionPerformed() 内的 if 语句未执行

java - 在 eclipse 中将 JFreeChart 添加到 Maven

python - 如何使 Tkinter 支持 PNG 透明度?

python - 如何将 Tkinter 安装到 Aptana Studio 35-32(非 Eclipse)

python - 字典联合并对另一个字段求​​和

python - 无法让 Theano 在 ubuntu 14.04 上工作

python - 基于多列分箱(分类值)的最佳方式

python - 使用Python获取DNS解析时间和响应时间

python - 首先是 Tkinter 还是 PyQt?

python - 如果某行包含特殊字符,如何以不同的名称保存文档?