tkinter中函数之间的跳跃

2024-09-30 20:20:51 发布

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

所以我正在做这个学校的项目,我需要能够运行随机数生成器,如果它是1,则转到headspinner模块,如果是2,则转到tailspinner模块。之后,代码应将分数相加并打印给用户。 任何关于如何在我的代码中从一个函数跳到另一个函数的建议都将不胜感激,谢谢

from tkinter import * # access tkinter library
import random # get random number generator


money = 100


#create coin flip module
def coinFlip():

    flip = random.randint(1, 2)
    if flip == 1:
        headspinner()
    else:
        tailspinner()
        
#create headspinner module
def headspinner():
    global money
    head = random.randint(1, 2)
    if head == 1:
        money = money + 30
    else:
        money = money - 25

#create tailspinner module
def tailspinner():
    global money
    tail = random.randint(1, 4)
    if tail == 1:
        money = money + 2
    elif tail == 2:
        money = money + 5
    elif tail == 3:
        money = money + 10
    else:
        money = money + 15

#gains or losses module
def upordown():
    global money
    if money > 100:
        screen.itemconfig(message, text = f"Congratulations, you won ${(money - 100):.2f}", font = ("Calibri", "18")) 
    else:
        screen.itemconfig(message, text = f"Congratulations, you won ${(100 - money):.2f}", font = ("Calibri", "18"))
    
   


#create pop up box
root = Tk()

#creating canvas
screen = Canvas (root, height = 600, width = 800, bg = "lightgreen")
screen.pack()

#flip button
go = Button(root, text = "Flip", cursor = "hand2", command = coinFlip)
goButton = screen.create_window(400, 530, window = go)

#cerate title
title = screen.create_text(400, 100, text = "Welcome to Flip and Spin", font = ("Calibri", "35"))


#text for instructions
text1 = Label(root, text = "Welcome to the Flip and Spin game! \nThis game includes a quick fate of what you could encounter. \nIt starts off with a fast flip of a coin followed by a dizzying spin of a spinner. \nCome and play to decide if you will lose money, \nmake your money back, or win some easy money!", font = ("Calibri", "20"), justify = CENTER, bg = "light green")
text1label = screen.create_window(400, 300, window = text1,)



#text for final results
message = screen.create_text(400, 400, text = "Results", font = ("Calibri", "18"))



#close canvas
mainloop()

Tags: textifdefcreaterandomscreenelsetail
1条回答
网友
1楼 · 发布于 2024-09-30 20:20:51

您已经在def coinFlip中从一个函数切换到另一个函数

你只需要把所有的东西放在一起

下面是一个简单的示例,让您开始学习:


from tkinter import Tk, Button, Label, X, LEFT, RIGHT
import random


money = 100


#create coin flip module
def coin_flip():
    flip = random.randint(1, 2)
    if flip == 1:
        headspinner()
    else:
        tailspinner()

#create headspinner module
def headspinner():
    global money
    head = random.randint(1, 2)
    if head == 1:
        money = money + 30
    else:
        money = money - 25
    text.config(text=f"Headspinner: {money}", bg="#aaafff")
    

#create tailspinner module
def tailspinner():
    global money
    tail = random.randint(1, 4)
    if tail == 1:
        money = money + 2
    elif tail == 2:
        money = money + 5
    elif tail == 3:
        money = money + 10
    else:
        money = money + 15
    text.config(text=f"Tailspinner: {money}", bg="#ffaaaa")

def show_result():
    global money
    text.config(text=f"Result: {money}.\nClick the Flip button to start over.", bg="#fffaaa")
    money = 100 # reset
    
        
root = Tk()
root.title("Game")

# the text here changes when the buttons are pressed
text = Label(root, text="Welcome to the Flip and Spin game!", font = ("Calibri", "18"))
text.pack()

rules = Label(root, text="This game includes a quick fate of what you could encounter. \nIt starts off with a fast flip of a coin followed by a dizzying spin of a spinner. \nCome and play to decide if you will lose money, \nmake your money back, or win some easy money!", font = ("Calibri", "10"))
rules.pack()

# click the button to run the command and change the text in the Label
flip_button = Button(root, text="Flip", command=coin_flip, fg="blue")
flip_button.pack(fill=X, side=LEFT, expand=True)

# click the button to run the command and change the text in the Label
result_button = Button(root, text="Result", command=show_result, fg="blue")
result_button.pack(fill=X, side=RIGHT, expand=True)

root.mainloop()

相关问题 更多 >