Python Forum

Full Version: Print if all checkboxes marked
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi guys,
I'm starting my adventure with python and well - programming at all and I've got a small problem for which I can't find any answer.

I wrote simple code that runs GUI with few Checkboxes. What I want to do is when all checkboxes are marked (checked) it will print in this GUI information - Well done! or something. Is it possible in Python? How to code it?

I would be greatfull if someone explain it to me instead of giving me links to tutorials - I read them and I couldn't find any answer or example of that.
It would be useful to show your code (in code tags).
Also, I don't see how providing a link to good tutorial is different from explaining it here. i.e. post on the forum would be sort of very short tutorial. :-)
(May-19-2017, 02:31 PM)Kaelmi Wrote: [ -> ]Is it possible in Python? How to code it?
Yes. Event handlers.

It's really hard to tell you how to do it, if we don't know what you've already done.
So I've got this code (it's not full cuz everything repeats):
from Tkinter import *
import tkMessageBox
import Tkinter

top = Tkinter.Tk()
top.wm_title("Checklist")

CheckVar1 = IntVar()
CheckVar2 = IntVar()

C1 = Checkbutton(top, text = "Underwear", variable = CheckVar1, anchor = W,  onvalue = 1, offvalue = 0, height=1, width = 40)
C2 = Checkbutton(top, text = "T-shirts", variable = CheckVar2, anchor = W, onvalue = 1, offvalue = 0, height=1, width = 40)

B1 = Tkinter.Button(top, text = "Sprawdz", command = confirm)

C1.pack()
C2.pack()

B1.pack()

top.mainloop()
What I want to do is when I press the button "Sprawdz" it will check whether all checkboxes are marked as checked. Whether they are  or not the message will appear in other gui window. How can I do that? I read something about those event handlers you mentioned above but I don't have any knowledge connected with IT and I couldn't find anything for "totally newbies". 

Thanks for response :)
you already did connected the B1 button with event-handler confirm, but didn't write it. Note that if you have number of check-boxes you can do something more elegant, rather than repeat code

from Tkinter import *
import tkMessageBox
 
top = Tkinter.Tk()
top.wm_title("Checklist")
my_items = ['Underwear', 'T_shirst', 'Socks', 'Hats'] # you can expand list of items/check-boxes
check_boxes = {item:IntVar() for item in my_items} #create dict of check_boxes

def confirm():
    print all(item.get() for item in check_boxes.values()) # just for info to show you the reasult
    if all(item.get() for item in check_boxes.values()):
        pass # do something you want

#create all check-boxes on the form
for item in my_items:
    C = Checkbutton(top, text = item, variable = check_boxes[item], anchor = W,  onvalue = 1, offvalue = 0, height=1, width = 40)
    C.pack()

B1 = Tkinter.Button(top, text = "Sprawdz", command = confirm)
B1.pack()

 
top.mainloop()
You don't need the confirm button at all if you'd like to eliminate that step.
instead, add command to each checkbox, have the event kick off the same confirm function
the confirm event can check to see if both boxes are checked and print  message if so.

here's a snippit not tested with your code that can do something like this:
I would containerize it all as well I used python 3.6 (2.7 is too old for me, you can change back)

import tkinter as tk


class Cbuttons:
   def __init__(self):
       self.root = tk.Tk()
       self.root.title("Checklist")
       self.CheckVar1 = tk.IntVar()
       self.CheckVar2 = tk.IntVar()
       self.main()

   def confirm(self):
       if self.CheckVar1.get() and self.CheckVar2.get():
           print('Both boxes checked')

   def main(self):
       C1 = tk.Checkbutton(self.root, text="Underwear", variable=self.CheckVar1, anchor='w', onvalue=1,
                           offvalue=0, height=1, width=40, command=self.confirm)
       C2 = tk.Checkbutton(self.root, text="T-shirts", variable=self.CheckVar2, anchor='w', onvalue=1,
                           offvalue=0, height=1, width=40, command=self.confirm)
       C1.pack()
       C2.pack()
       self.root.mainloop()


if __name__ == '__main__':
   Cbuttons()
Timing issue, I was writing code when Braun was posting
Thank you guys, now I got how it works after a moment of analizing your codes. Thank you again! :)
What I want to do is to create a "Master Menu" by Tkinter in python. In that menu there would be like 2 or 3 buttons which will open new windows with different checklists etc. How can it be done? I was looking for solution for the last 2 hours and the only thing I got is creating new window with label. I don't know how to make python to open checklist in new Tkinter window. Can you help me?

I know that Tkinter.Toplevel() opens new window but how to "import" the previously created checklist into that window? Let's say I've got such list of things to do:

software = ['MS Office', 'Drivers', 'Chrome', 'ESET']
I did everything like you did in the code you gave me:


import Tkinter
from Tkinter import *
import tkMessageBox

top = Tkinter.Tk()

software = ['MS Office', 'Drivers', 'Chrome', 'ESET']

check_boxes_software = {item:IntVar() for item in software}

def software():
    if all(item.get() for item in check_boxes_software.values()):
        tkMessageBox.showinfo('Checklista Software', 'Wszystko gotowe!')
    else:
        tkMessageBox.showinfo('Checklista Software', 'Czegos brakuje!')

for item in software:
    C = Checkbutton(top, text = item, variable = check_boxes_software[item], anchor = W,  onvalue = 1, offvalue = 0, height=1, width=40)
    C.pack()

B1 = Tkinter.Button(top, text ='Sprawdz', command = software)

B1.pack()

top.mainloop()
How to make list above shows in another Tkinter window?
Nevermind, I've got it working. I just assigned command to button, and defined that command to open another python script. Example:

def another_script():
    os.system("python Anotherscript.py")

B1 = tk.Button(top, text = "Software Checklist", command = another_script)
Remember to import sys and os to your code. Hope this helps others :)
Pages: 1 2