或者懒惰的方式..
我正在寻找一个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()
但我刚收到错误
can't invoke "listbox" command: application has been destroyed
尝试了很多不同的选项,我会解决这个问题(比如使用全局变量) - 但没有任何运气。
@blablatros给了我正确的解决方案。
答案 0 :(得分: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)
choice
将返回所选答案的列表。
将multchoicebox
用于多项选择题,或choicebox
用于单项选择。
答案 1 :(得分: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()
更新:
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
主脚本:
from GetUserInput import GetUserInput
listOfOptions = ["option 1", "option 2", "option 3"]
print GetUserInput(listOfOptions, True).getInput()
希望有所帮助。
答案 2 :(得分:4)
我重复了@ alecxe的回答,使用OOP以更强大的方式管理GUI生命周期:
# This serve as a module to get user input - the easy way!
# Some GUI selection
import Tkinter
default_kwargs = {
'selectmode' : "single" ,
'width' : "150" ,
'height' : "30" ,
'title' : "Choose from list",
'buttonText' : "Submit"
}
class easyListBox:
def __init__(self, options_list, **kwargs) :
#options
opt = default_kwargs #default options
opt.update(kwargs) #overrides default if existant
#Return value
self.selected = 0;
# GUI master object (life-time component)
self.master = Tkinter.Tk()
# Checklist with options
listbox_options = { key: opt[key] for key in opt if key in['selectmode','width','height'] } #options slice for GUI
self.listbox = Tkinter.Listbox(self.master, listbox_options)
self.listbox.master.title(opt['title'])
#Options to be checked
for option in options_list:
self.listbox.insert(0,option)
self.listbox.pack()
# Submit callback
self.OKbutton = Tkinter.Button(self.master, command=self.OKaction, text=opt['buttonText'] )
self.OKbutton.pack()
#Main loop
self.master.mainloop()
# Action to be done when the user press submit
def OKaction(self):
self.selected = self.listbox.selection_get()
self.master.destroy()
# Return the selection
def getInput(self):
return self.selected
#import GetUserInput
import GUI as GetUserInput
listOfOptions = ["option 1", "option 2", "option 3"]
GUI_options = {'title' : "Custom title", 'selectmode' : 'multiple' }
#choice = GetUserInput.chooseFromList(listOfOptions)
elb = GetUserInput.easyListBox(listOfOptions, **GUI_options)
print elb.getInput()
为了处理变量参数,我添加了一些默认参数kwargs。
PS:我使用的是Python 2.7,所以必须调整一些值(例如MULTIPLE - >'multiple')
答案 3 :(得分:3)
import Tkinter
def callback(master, listbox, selection):
selection[:] = [listbox.get(i) for i in map(int, listbox.curselection())]
master.destroy()
def chooseFromList(options, windowTitle="Choose from list", buttonText="Submit", selectMultiple=False, w=150, h=30):
master = Tkinter.Tk()
master.title(windowTitle)
listbox = Tkinter.Listbox(master, selectmode=Tkinter.MULTIPLE if selectMultiple else Tkinter.SINGLE, width=w, height=h)
for option in options:
listbox.insert(Tkinter.END, option)
listbox.pack()
selection = []
Tkinter.Button(master, command=lambda: callback(master, listbox, selection), text=buttonText).pack()
master.mainloop()
return selection
对多个选项使用Checkbutton,单选项使用Radiobutton。
def chooseFromList(options, windowTitle="Choose from list", buttonText="Submit", selectMultiple=False, w=150, h=30):
master = Tkinter.Tk()
master.title(windowTitle)
variables = []
if selectMultiple:
for option in options:
v = Tkinter.StringVar()
variables.append(v)
Tkinter.Checkbutton(text=option, variable=v, onvalue=option, offvalue='').pack()
else:
v = Tkinter.StringVar()
variables.append(v)
for option in options:
Tkinter.Radiobutton(text=option, variable=v, value=option).pack()
Tkinter.Button(master, command=master.destroy, text=buttonText).pack()
master.mainloop()
return [v.get() for v in variables if v.get()]
答案 4 :(得分:1)
嗯,你不会快速得到 的东西,几乎无论你在哪里我都不会想到。通常,您至少需要足够的样板来创建顶级窗口和/或窗口小部件来布置您实际关注的输入窗口小部件。
Python对GTK2和Qt(PyQt,现在使用4.X)都有很好的绑定,这两种高质量的跨平台GUI工具包都很容易上手。还有其他人,wxWidgets是另一个突出的,但其余的(包括内置的IMO)已经过时了。
答案 5 :(得分:1)
答案 6 :(得分:1)
从这里你可以得到确切的答案。只需点击此处http://www.blog.pythonlibrary.org/2013/02/27/wxpython-adding-checkboxes-to-objectlistview/
即可