Python:使用按钮和标签显示函数返回的文本

2024-05-26 04:24:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个程序来生成不同的练习,以帮助减肥。函数生成一个练习,以及你应该做的练习的数量。我在tkinter窗口中使用标签显示这些

窗口中还有一个按钮,单击该按钮时应调用函数,并在标签中显示练习和数量

有可能这样做吗

from tkinter import *
import random as r

exercises = ["star jumps", "press ups", "sit ups", "burpees", "bicycle crunches"] #array of exercises
eChoice = "" #declare variable for exercise choice
amount = "" #declare variable for amount of exercises
def exercise():
    x = r.randint(1, 4)
    if x == 1:
        #exercise
        eChoice = r.choice(exercises)
        amount = r.randint(10, 30)
    elif x == 2:
        #walk
        eChoice = "walk"
        amount = r.randint(1,3) #miles
    elif x == 3:
        #cycle
        eChoice = "cycle"
        amount = r.randint(1,3) #miles
    else:
        #swim
        eChoice = "swim"
        amount = r.randint(5,10) #lengths of a pool
    return eChoice, amount


myGUI = Tk() #create window

myGUI.title("Weight Loss Program") #title of the window
myGUI.configure(bg="deepskyblue") #background colour
exerciseButton = Button(myGUI, text="Generate Exercise", fg="mediumpurple", command=exercise) #create button that calls exercise function
exerciseButton.pack()
name = "Name: ", eChoice
exerciseLabel = Label(myGUI, text=name, fg="mediumpurple") #create label for exercise name
exerciseLabel.pack()
description = "Amount: ", amount
exerciseDescription = Label(myGUI, text=description, fg="mediumpurple") #create label for exercise name
exerciseDescription.pack()

myGUI.mainloop()```

Tags: oftextnamefor数量createamountpack
1条回答
网友
1楼 · 发布于 2024-05-26 04:24:08

您的exercise函数正在将值返回给调用者。应该修改函数中需要修改的变量,而不是返回值。在本例中,我对变量进行了全局引用,但也可以将它们作为参数包含到函数中,以便在局部范围内访问它们

解决方案:

from tkinter import *
import random as r

exercises = ["star jumps", "press ups", "sit ups", "burpees", "bicycle crunches"] #array of exercises
eChoice = "" #declare variable for exercise choice
amount = "" #declare variable for amount of exercises
def exercise():
    global exerciseLabel
    global exerciseDescription
    global eChoice

    x = r.randint(1, 4)
    if x == 1:
        #exercise
        eChoice = r.choice(exercises)
        amount = r.randint(10, 30)
    elif x == 2:
        #walk
        eChoice = "walk"
        amount = r.randint(1,3) #miles
    elif x == 3:
        #cycle
        eChoice = "cycle"
        amount = r.randint(1,3) #miles
    else:
        #swim
        eChoice = "swim"
        amount = r.randint(5,10) #lengths of a pool


    exerciseLabel.configure(text="Name: " + eChoice)
    exerciseDescription.configure(text="Amount: " + str(amount))


myGUI = Tk() #create window

myGUI.title("Weight Loss Program") #title of the window
myGUI.configure(bg="deepskyblue") #background colour
exerciseButton = Button(myGUI, text="Generate Exercise", fg="mediumpurple", command=exercise) #create button that calls exercise function
exerciseButton.pack()
name = "Name: " + eChoice
exerciseLabel = Label(myGUI, text=name, fg="mediumpurple") #create label for exercise name
exerciseLabel.pack()
description = "Amount: " + amount
exerciseDescription = Label(myGUI, text=description, fg="mediumpurple") #create label for exercise name
exerciseDescription.pack()

myGUI.mainloop()

相关问题 更多 >