Home > Blockchain >  Using if statements in Tkinter won't output on click
Using if statements in Tkinter won't output on click

Time:04-01

I am trying to make my first GUI app in python, and am very new to this. I want to make something for my wife (teacher) such that she can enter the number of "Growth Points" a student earns on a Standardized Test and spits out the number of "Gotchas" (in-class currency as incentives) the student receives.

The rules are: 3 gotchas for the first 6 growth points, then 5 gotchas for each subsequent growth point.

I was following a Guide from Geeksforgeeks to make the app, and can get the button to change text of a label already created but not output the Gotchas earned by the student... This is purely just to learn how to do it, so things like the Menu option is not necessary, just included as I learned.

Here is the code I have tried:

# Import Module
from tkinter import *

# create root window
root = Tk()

# root window title and dimension
root.title("Welcome to my first App")
# Set geometry (widthxheight)
root.geometry('450x300')

# all widgets will be here

# Determine the number of Growth Points
lbl = Label(root, text="How many Growth Points did the Student Earn?")
lbl.grid()

# Gather the number of Growth Points from the User
growth_points = Entry(root, width=10)
growth_points.grid(column=1, row=0)

menu = Menu(root)
item = Menu(menu)
item.add_command(label='New')
menu.add_cascade(label='File', menu=item)
root.config(menu=menu)

# Function for when the button is clicked
def clicked():
    lbl.configure(text = "Clicked!")  # Just to check whether the button does something
    growth_points = int(growth_points)
    if growth_points <= 6:
        lbl_test = Label(root, text="Under 6")  # Test to see if the if statement works
        lbl_test.grid(column=0,row=2)  # Output for the if statement (Doesn't work?)
        num_gotcha = growth_points*3  # Gives the number of Gotchas for the first 6 growth points
        num_gotcha = str(num_gotcha)  # Converts into a String for Printing
        gp_lbl = Label(root, text=num_gotcha)  # Labels and inserts after clicking button
        gp_lbl.grid(column=0,row=3)  
    elif growth_points > 6:
        over_gotcha = growth_points - 6  # Gets the amount of Growth Points over the first 6
        num_gotcha = over_gotcha * 5   18  # Finds the Gotchas for the GP's over 6, then adds the GPs for the first 6
        num_gotcha = str(num_gotcha)
        gp_lbl2 = Label(root, text="Student gets"   " "   num_gotcha   " "   "Gotchas")  # Another way of trying to display the value
        gp_lbl2.grid(column=0, row=3)
    

# Adding a button to begin the program
btn = Button(root, text = "Enter" , command=clicked)
btn.grid(column=2, row=0)


# Execute Tkinter
root.mainloop()

I can get the button to change the text, so it seems like the button works but it won't go through the If statements.. How should this work? Thanks for any help!

CodePudding user response:

Didn't meddle with the "Gothcas" logic. Just added global reference to entry widget and renamed the variable for entered growth points:

# Function for when the button is clicked
def clicked():
    global growth_points
    lbl.configure(text="Clicked!")  # Just to check whether the button does something
    growth_points_number = int(growth_points.get())
    if growth_points_number <= 6:
        lbl_test = Label(root, text="Under 6")  # Test to see if the if statement works
        lbl_test.grid(column=0, row=2)  # Output for the if statement (Doesn't work?)
        num_gotcha = growth_points_number * 3  # Gives the number of Gotchas for the first 6 growth points
        num_gotcha = str(num_gotcha)  # Converts into a String for Printing
        gp_lbl = Label(root, text=num_gotcha)  # Labels and inserts after clicking button
        gp_lbl.grid(column=0, row=3)
    elif growth_points_number > 6:
        over_gotcha = growth_points_number - 6  # Gets the amount of Growth Points over the first 6
        num_gotcha = over_gotcha * 5   18  # Finds the Gotchas for the GP's over 6, then adds the GPs for the first 6
        num_gotcha = str(num_gotcha)
        gp_lbl2 = Label(root,
                        text="Student gets"   " "   num_gotcha   " "   "Gotchas")  # Another way of trying to display the value
        gp_lbl2.grid(column=0, row=3)

That should work.

  • Related