dimanche 9 février 2020

Tkinter / Python 3.7 - How to set checkboxes as mandatory - if user don't check - a popup should appear on the screen

What i am trying to do:

i am trying to create a small program in tkinter where it calculate the fees for a client based on the client characteristics the user have to checked some checkboxes. Each checkbox has a score value that will be sum when the user will push the button and that sum will be multiplied by £500 (each point is £500 in fees)

The issue:

a group of 3 checkboxes (with variable = var2) needs to be selected before the user press the button "Calculate the fees". I would like that if the user forget to select one of the checkboxes in variable = var 2, a popup should come up on the screen where it says: "Error, you have not checked a mandatory checkbox, please check again before calculate the fees"

In the program that i am trying to do there are other 4 or 5 groups of checkboxes which need to be selected one time before calculate the fees (here i just pasted part of the program otherwise it too long)

What i have tried:

in the def addtolist() i tried to put an if statement to check if, before the program calculate the 
fees, the checkboxes with var2 has been selected once. Unfurtunately this does not work, here the 
error message from python:

Exception in Tkinter callback Traceback (most recent call last):
File "C:\Users\sheha\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 508, in get return self._tk.getint(value) _tkinter.TclError: expected integer but got ""

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Users\sheha\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 1705, in call return self.func(*args) File "test_final.py", line 20, in addtolist if var2.get() != "": File "C:\Users\sheha\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 510, in get return int(self._tk.getdouble(value)) _tkinter.TclError: expected floating-point number but got ""

Based on this error, if i try to change the offvalue of the checkboxes with var2 from "" to 0 (zero), one of the boxes in that group will be automatically selected when i run the program (i would like to avoid the default selection if it is possible)

Can you please help me?

Please find the code below Thank you

import tkinter as tk

from tkinter import *

from tkinter import messagebox

# --- functions ---

def on_configure(event):
    # update scrollregion after starting 'mainloop'
    # when all widgets are in canvas
    canvas.configure(scrollregion=canvas.bbox('all'))


def addtolist():
    global List

    List = []

    if var2.get() != "":
        for item in varList:
            if item.get() != "":
                List.append(item.get())
        sum_list =sum(List)
        fees=sum_list*500
        messagebox.showinfo('Fee calculated', 'This client has scored %s points and the fees for the audit job is: £%s' %(sum_list, fees))
    else:
        messagebox.showerror('Mandatory checkboxe has been skipped', 'Error, you have not checked a mandatory checkbox, please check again before calculate the fees')    

# --- main ---

List = []
varList = []


root = tk.Tk()
root.title("Audit Pricing Tool")
root.geometry("500x500")

# --- create canvas with scrollbar ---

canvas = tk.Canvas(root)
canvas.pack(side=tk.LEFT, fill='both', expand=True)

scrollbar = tk.Scrollbar(root, command=canvas.yview)
scrollbar.pack(side=tk.LEFT, fill='y')

canvas.configure(yscrollcommand = scrollbar.set)

# update scrollregion after starting 'mainloop'
# when all widgets are in canvas
canvas.bind('<Configure>', on_configure)

# --- put frame in canvas ---

frame = tk.Frame(master=root, width=980, height=980)
canvas.create_window((0,0), window=frame, anchor='nw')
#frame.pack_propagate(0) #Don't allow the widgets inside to determine the frame's width / height
#frame.pack(fill=tk.BOTH, expand=True) #Expand the frame to fill the root window

# --- add widgets in frame ---

label_title=Label(frame, text="Pricing Tool", height=3, fg="blue", font=(20))
label_title.grid(row=1, column=1, columnspan=2 , sticky=N)

#this checkbox need to be selected by default

var1 = IntVar()
cb1 = Checkbutton(frame, text="Base Price (applies to all)", variable=var1,
                           onvalue=15, offvalue="")
cb1.grid(row=11, column=1, sticky=W)
cb1.select()


#label for Appointment/reappointment (need to select only one of these 3 checkboxes before calculate the fees
# if the user skip this section and try to calculate the fees - it should get an error pop up and tell to the use
# to check on of the mandatory boxes
# )
lb1=Label(frame, text="Appointment/reappointment (select one)", height=3)
lb1.grid(row=13, column=1, sticky=N)

var2 = IntVar()
cb2 = Checkbutton(frame, text="taking over from another auditor", variable=var2,
                         onvalue=5, offvalue=0)
cb2.grid(row=14, column=1,sticky=W)

cb3 = Checkbutton(frame, text="First year that accounts require audit", variable=var2,
                         onvalue=3, offvalue=0)
cb3.grid(row=15, column=1,sticky=W)

cb4 = Checkbutton(frame, text="Continuing engagement", variable=var2,
                         onvalue=0, offvalue=0)
cb4.deselect()
cb4.grid(row=16, column=1,sticky=W)


# for the checkboxes below, you can select or not select (it is not mandatory)

lb8=Label(frame, text="Additional complexities (select all that apply)", height=3)
lb8.grid(row=66, column=1, sticky=N)

var9 = IntVar()
cb38 = Checkbutton(frame, text="Accounts prepared under IFRS / FRS 101", variable=var9,
                         onvalue=2, offvalue=0)
cb38.grid(row=67, column=1,sticky=W)

var10 = IntVar()
cb39 = Checkbutton(frame, text="Change of accounting software", variable=var10,
                         onvalue=2, offvalue=0)
cb39.grid(row=68, column=1,sticky=W)

var11 = IntVar()
cb40 = Checkbutton(frame, text="Stock", variable=var11,
                         onvalue=1, offvalue=0)
cb40.grid(row=69, column=1,sticky=W)

var12 = IntVar()
cb41 = Checkbutton(frame, text="Stocktake attendance", variable=var12,
                         onvalue=2, offvalue=0)
cb41.grid(row=70, column=1,sticky=W)


var24 = IntVar()
cb50 = Checkbutton(frame, text="Turnover < £1 million", variable=var24,
                         onvalue=-1, offvalue=0)
cb50.grid(row=86, column=1,sticky=W)


varList.append(var1)
varList.append(var2)
varList.append(var9)
varList.append(var10)
varList.append(var11)
varList.append(var12)
varList.append(var24)

b1 = Button(frame, text="Calculate fees", command=addtolist)
b1.grid(row=100, column=1, sticky=N)


frame.mainloop()

root.mainloop()



Aucun commentaire:

Enregistrer un commentaire