繁体   English   中英

在 Tkinter 中使用 if 语句不会在点击时出现 output

[英]Using if statements in Tkinter won't output on click

我正在尝试在 python 中制作我的第一个 GUI 应用程序,对此我很陌生。 我想为我的妻子(老师)做点什么,这样她就可以输入学生在标准化考试中获得的“成长点”的数量,并吐出学生收到的“陷阱”(作为奖励的课堂货币)的数量.

规则是:前 6 个增长点有 3 个陷阱,然后每个后续增长点有 5 个陷阱。

我正在按照Geeksforgeeks的指南制作应用程序,并且可以获得更改文本的按钮 label 已经创建但不是 output 学生获得的 Gotchas ...这纯粹是为了学习如何做,所以事情就像菜单选项不是必需的,只是据我所知包括在内。

这是我试过的代码:

# 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()

我可以获得更改文本的按钮,所以看起来按钮可以工作,但它不会通过 If 语句 go ..这应该如何工作? 谢谢你的帮助!

没有干涉“Gothcas”逻辑。 刚刚添加了对入口小部件的全局引用,并为输入的增长点重命名了变量:

# 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)

那应该有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM