lilmae23
lilmae23

Reputation: 1

How do I get my main code to work in a pysimplegui application

I am very new to python and GUI applications so I am getting quite confused regularly. I have created a pysimplegui application for a fake gym membership application and need certain components. The GUI itself is working fine but the calculation code function doesn't work. When the button 'Calculate Membership Total' is pressed, it is meant to perform a calculation of the costs involved. But when it is pressed nothing happens. I am not sure whether I have not called it correctly or if I have done it completely wrong and need to restart. I am also stuck on how to correctly name and connect what the users have selected to what needs to be calculated.

This is the code I have done so far. Any small bit of help is appreciated!

import PySimpleGUI as sg

 sg.theme('LightGray1') #add colour
default_color = 'dark red'

#create columns for details/information
col1 = sg.Column([
[sg.Text("City Gym Membership Form", size = (30,1), font = ('Helvetica' , 30), text_color = 'red')],
])

col2 = sg.Column([
[sg.Text('Customer Details:', size = (15,1), font = ("Helvetica", 20), text_color = 'dark red', key = 'Customer details')],
[sg.Text('First Name:', key = 'First Name')],
[sg.InputText()],
[sg.Text('Last Name:', key = 'Last Name')],
[sg.InputText()],
[sg.Text('Address:', key = 'Address')],
[sg.InputText()],
[sg.Text('Mobile Number:', key = 'Mobile Number')],
[sg.InputText()],
])

col3 = sg.Column([
[sg.Text('Membership Details:', font = ('Helvetica', 20), text_color = 'dark   red', key = 'Membership details')],
[sg.Text('Type of membership:', key = 'Member Type')],
[sg.Radio('Basic($10pw)', 'Radio1', default = True, key = 'basic'),
sg.Radio('Regular($15pw)', 'Radio1', key = 'regular'),
sg.Radio('Premium($20pw)', 'Radio1', key = 'premium')],
])

col4  = sg.Column([
[sg.Text('Duration:', key = 'duration')],
[sg.Radio('3 months',  'Radio2', default = True, key = '3months'),
sg.Radio('12 months($2pw discount)', 'Radio2', key = 'twelvemonths'),
sg.Radio('24 months($5pw discount)', 'Radio2', key = 'twentyfourmonths')],
])

col5 = sg.Column([
[sg.Text('Payment Option:', key = 'Payment Option')\],
[sg.Text('Direct Debit(1% discount on any membership type):', key = 'Direct Debit')],
[sg.Radio('Yes', 'Radio3', default = True, key = 'yes'),
sg.Radio('No', 'Radio3', key = 'no')\],
])

col6 = sg.Column([
[sg.Text('Frequency of payment:', key = 'frequency')],
[sg.Radio('Weekly', 'Radio4', default = True, key = 'weekly'),
  sg.Radio('Monthly', 'Radio4', key = 'monthly')],
 ])

col7 = sg.Column([
[sg.Text('Extras:', size = (0,1), font = ("Helvetica", 20), text_color = 'dark red', key = 'extras')],
[sg.Checkbox('24/7 access($1 per week)', key = 'access')],
[sg.Checkbox('Personal Trainer($20 per week)', key = 'personal_trainer')],
[sg.Checkbox('Diet Consultation($20 per week)', key = 'diet')],
[sg.Checkbox('Access online fitness videos($2 per week)', key = 'videos')],
])

#create calculation code

def calculate_membership_total (Type_of_Membership, extras, discounts, Frequency_of_payment):
membtype = Type_of_Membership
cost1 = membtype.isdecimal()
basic = 'Basic($10pw)', 'Radio1'
regular = 'Regular($15pw)', 'Radio1'
premium = 'Premium($20pw)', 'Radio1'

if cost1 == basic :
    print(10)
if cost1 == regular :
    print(15)
if cost1 == premium :
    print(20)

xtras = extras
cost2 = xtras.isdecimal()
access = sg.Checkbox('24/7 access($1 per week)')
personal_trainer = sg.Checkbox('Personal Trainer($20 per week)')
diet = sg.Checkbox('Diet Consultation($20 per week)')
videos = sg.Checkbox('Acess online fitness videos($2 per week)')

if access == 'True':
    print(1)
if personal_trainer == 'True':
    print(20)
if diet == 'True':
    print(20) 
if videos == 'True':
    print(2)

discount1 = discounts
cost3 = discount1.isdecimal() 
twelvem = '12months($2pw discount)', 'Radio2'
twentyfourm = '24months($5pw discount)', 'Radio2'
debit1 = 'Yes', 'Radio 3'
debitdiscount = 1 *  int(cost1) / 100

if twelvemonths == 'True':
    print(2)
if twentyfourmonths == 'True': 
    print(5)
if debit1 == 'Yes':
    print(debitdiscount)

Net_total = cost1 + cost2 - cost3
cost4 = Frequency_of_payment 
weekly = 'weekly' , 'Radio4'
monthly = 'monthly', 'Radio4'

if cost4 == weekly:
    print(Net_total)
if cost4 == monthly:
    print (Net_total * 4)

col8 = sg.Button('Calculate Membership Total',    calculate_membership_total('Type_of_Membership', 'extras', 'discounts',  'Frequency_of_payment'), button_color = 'red', key = 'calculate')

#create layout
layout = [[col1],
[col2, col7],
[col3, col8, sg.Exit('Submit')],
[col4],
[col5],
[col6],
]

#create the window
window = sg.Window('City Gym Membership Form', layout)

#create an event loop
while True:
event, values = window.read()
print(event, values)
if event in (sg.WIN_CLOSED, 'Exit'): #if user closes window
break
if event == 'Calculate Membership Total':
calculate_membership_total()
if event == 'submit':
print('Your application has been submitted!')

window.close()

Upvotes: 0

Views: 56

Answers (1)

Jason Yang
Jason Yang

Reputation: 13051

The code you provided is with poor indentation.

col8 = sg.Button(
    'Calculate Membership Total',
    # calculate_membership_total('Type_of_Membership', 'extras', 'discounts',  'Frequency_of_payment'),
    button_color = 'red',
    key = 'calculate')

if event == 'Calculate Membership Total':
    calculate_membership_total()

The Button Calculate Membership Total with key defined as 'calculate', so you will get an event 'calculate' when clicked, not the event 'Calculate Membership Total' you checked in the above if statement, so the function calculate_membership_total() not called.

Upvotes: 0

Related Questions