在 Tkinter 中使用 if 语句不会在点击时输出
Using if statements in Tkinter won't output on click
我正在尝试在 python 中制作我的第一个 GUI 应用程序,对此我还很陌生。我想为我的妻子(老师)做点什么,这样她就可以输入学生在标准化测试中获得的“成长点数”,并吐出“陷阱”的数量(in-class 货币作为奖励) 学生收到。
规则是:前6个成长点3个陷阱,之后每个成长点5个陷阱。
我是按照 Geeksforgeeks 的指南来制作应用程序的,并且可以获得更改已创建标签文本的按钮,但不会输出学生获得的 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 语句。这应该如何工作?感谢您的帮助!
没有干涉“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)
应该可以。
我正在尝试在 python 中制作我的第一个 GUI 应用程序,对此我还很陌生。我想为我的妻子(老师)做点什么,这样她就可以输入学生在标准化测试中获得的“成长点数”,并吐出“陷阱”的数量(in-class 货币作为奖励) 学生收到。
规则是:前6个成长点3个陷阱,之后每个成长点5个陷阱。
我是按照 Geeksforgeeks 的指南来制作应用程序的,并且可以获得更改已创建标签文本的按钮,但不会输出学生获得的 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 语句。这应该如何工作?感谢您的帮助!
没有干涉“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)
应该可以。