Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: get a checkbox - the easiest way

or perhaps the lazy way..

I'm looking for a python module that has some build-in GUI methods to get quick user inputs - a very common programming case. Has to work on windows 7

My ideal case

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

It's kinda like raw_input but with a GUI. There must be something out there since this is a common programming task.


UPDATE

@alecxe it is not to be rude that I unchecked your answer as the solution to my problem. I still want to be able to use my ideal case in whatever script I'm working on and your answer gets me half the way.

I thought that I could implement @alecxe's solution easily into a module, but it's not that simple (for me)..

Here is my module so far:

# 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

And here is my script

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

But I just get the error

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

Have tried a lot of different options that I though would solve the case (like using global variable) - but without any luck.

UPDATE 2

@blablatros gave me exactly the solution that I was looking for.

like image 665
Norfeldt Avatar asked Jun 19 '13 07:06

Norfeldt


People also ask

How to get checkbox value in Python?

Build A Paint Program With TKinter and Python To print the value of the selected checkbox, we can use the get() method. It returns the input value of a particular widget.

How do I check if a checkbox is checked in python flask?

If you want to know if its checked on the server side, just check if that form field exists, if request. form. get("name") gives you NULL or exception, then the checkbox should be unchecked.


1 Answers

Easygui module is exactly what you need:

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 will return a list of selected answers.

Use multchoicebox for multiple choice question, or choicebox for a single choice.

like image 151
blablatros Avatar answered Oct 16 '22 13:10

blablatros