我正在嘗試用 python 制作我的第一個 GUI 應用程式,對此我很陌生。我想為我的妻子(老師)做一些事情,這樣她就可以輸入學生在標準化測驗中獲得的“成長點”的數量,并吐出學生收到的“陷阱”(課堂貨幣作為獎勵)的數量.
規則是:前 6 個增長點 3 個陷阱,之后每個增長點 5 個陷阱。
我正在按照Geeksforgeeks的指南制作應用程式,并且可以獲得按鈕來更改已創建的標簽的文本,但不能輸出學生獲得的陷阱......這純粹是為了學習如何去做,所以事情像選單選項一樣不是必需的,只是據我所知包括在內。
這是我嘗試過的代碼:
# 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!
uj5u.com熱心網友回復:
沒有干涉“哥特卡”的邏輯。剛剛添加了對條目小部件的全域參考,并為輸入的增長點重命名了變數:
# 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)
那應該行得通。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/453876.html
標籤:python if-statement tkinter
