【问题标题】:Python: get a checkbox - the easiest wayPython:获取一个复选框 - 最简单的方法
【发布时间】:2013-06-19 07:19:58
【问题描述】:

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

我正在寻找一个 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

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

更新 2

@blablatros 为我提供了我正在寻找的解决方案。

【问题讨论】:

    标签: python user-interface tkinter


    【解决方案1】:

    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 程序员。非常感谢
    • 懒惰的程序员有福了!很高兴我能帮上忙。
    • 我知道几年前你们为此感到高兴,但我只是唱了我自己的赞美之词。最后,快速简单的事情!
    【解决方案2】:

    这是一个使用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()
    

    希望对您有所帮助。

    【讨论】:

    • 我可以将它封装在个人 python 模块 (magicGUImodule) 中的函数中——它基本上只会调用你的代码 :)
    • 不客气,当然。我敢打赌,它在任何 python gui 工具中都会像 wxqt 一样简单。
    • 如何从 Tkinter 挖掘 MULTIPLE 参数?我想创建一个类似的列表: mode = [SelectMode.SINGLE, SelectMode.MULTIPLE] // listbox = Listbox(master, selectmode=mode[True], width=w, height=h)
    • 我在创建模块时遇到了一些小的 OOP 问题。所以不得不重新打开这个问题..对不起。
    • 嗯,最后一个解决方案使用类变量而不是全局每个脚本变量,它更 Pythonic 和干净。对于其他易于调用的预定义方法,只需使用此类作为模板。希望对您有所帮助。
    【解决方案3】:

    我已经迭代了@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')

    【讨论】:

      【解决方案4】:

      列表框

      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()]
      

      【讨论】:

        【解决方案5】:

        从这里你可能会得到准确的答案。只需点击这里http://www.blog.pythonlibrary.org/2013/02/27/wxpython-adding-checkboxes-to-objectlistview/

        【讨论】:

          【解决方案6】:

          Tkinter 是在 Python 中构建的,具有您上面所说的形式的复选框,并且比大多数其他 GUI 模块更简单。请找到一个好的教程(虽然需要一些刷新)here。官方文档是here

          【讨论】:

            【解决方案7】:

            嗯,你不会很快得到一些东西那么,几乎不管你在哪里看,我不认为。通常,您至少需要足够的样板来创建顶级窗口和/或小部件来布局您真正关心的输入小部件。

            Python 为 GTK2 和 Qt(PyQt,目前使用 4.X)提供了很好的绑定,这两个工具包都非常高质量,易于上手。还有其他的,wxWidgets 是另一个突出的,但其余的(包括内置的 IMO)已经过时了。

            【讨论】:

              猜你喜欢
              • 2018-05-06
              • 2011-06-08
              • 1970-01-01
              • 2015-05-21
              • 2013-01-16
              • 1970-01-01
              • 2011-01-09
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多