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

Nor*_*ldt 10 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)
Run Code Online (Sandbox Code Playgroud)

它有点像raw_input但有GUI.必须有一些东西,因为这是一个常见的编程任务.


UPDATE

@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
Run Code Online (Sandbox Code Playgroud)

这是我的剧本

import GetUserInput
listOfOptions = ["option 1", "option 2", "option 3"]
choice = GetUserInput.chooseFromList(listOfOptions)
print choice.getInput()
Run Code Online (Sandbox Code Playgroud)

但我只是得到错误

can't invoke "listbox" command: application has been destroyed

尝试了很多不同的选项,我会解决这个问题(比如使用全局变量) - 但没有任何运气.

更新2

@blablatros给了我正确的解决方案.

bla*_*ros 10

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)
Run Code Online (Sandbox Code Playgroud)

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

使用multchoicebox多选问题,或choicebox单个的选择.


ale*_*cxe 7

这是一个使用的简单示例Tkinter(而不是使用listbox具有多个选择的复选框):

from Tkinter import *


def callback():
    print listbox.selection_get()
    master.destroy()


master = Tk()

listbox = Listbox(master, selectmode=MULTIPLE)
for option in ["option 1", "option 2", "option 3"]:
    listbox.insert(0, option)
listbox.pack()

b = Button(master, command=callback, text="Submit")
b.pack()

mainloop()
Run Code Online (Sandbox Code Playgroud)

更新:

GetUserInput.py:

from Tkinter import *


class GetUserInput(object):
    selection = None

    def __init__(self, options, multiple):
        self.master = Tk()

        self.master.title("Choose from list")

        self.listbox = Listbox(self.master, selectmode=MULTIPLE if multiple else SINGLE, width=150, height=30)
        for option in options:
            self.listbox.insert(0, option)
        self.listbox.pack()

        b = Button(self.master, command=self.callback, text="Submit")
        b.pack()

        self.master.mainloop()

    def callback(self):
        self.selection = self.listbox.selection_get()
        self.master.destroy()

    def getInput(self):
        return self.selection
Run Code Online (Sandbox Code Playgroud)

主要脚本:

from GetUserInput import GetUserInput

listOfOptions = ["option 1", "option 2", "option 3"]
print GetUserInput(listOfOptions, True).getInput()
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.