如何修复Tkinter海滩死亡之球?

2022-08-10 12:59:15 发布

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

我想在Tkinter做RPS(岩石剪纸机)。我的代码工作完全正常,直到我按下OK按钮,当我这样做时,我得到旋转加载图标thingy(MacOS)。我试着在活动监视器中查看我的cpu,结果发现cpu的使用率达到了100%

我曾尝试用谷歌搜索这个问题,并添加了time.sleep(0.2),但没有成功

from tkinter import *
from random import randint
from tkinter import messagebox
import time

def rock():
    masterrock = Tk()

    rl = Label(masterrock, text="rock, paper or scissor")
    rl.grid(row=0, column=1)

    rb = Button(masterrock, text="OK", command=lambda: rock2())
    rb.grid(row=2, column=1)

    player1 = Entry(masterrock)
    player1.grid(row=1, column=1)
    global player
    player = (player1.get())

    t = ["rock", "paper", "scissor"]

    global computer
    computer = t[randint(0, 2)]
    
def rock2():
    s = True
    while s == True:

        if player == computer:
            gllabel = Label(masterrock, text="tie")
            gllabel.grid(row=2, column=1)

        elif player == "rock":

            if computer == "paper":
                gllabel1 = Label(masterrock, text="you lose!")
                gllabel1.grid(row=2, column=1)

            else:
                gllabel2 = Label(masterrock, text="You win!")
                gllabel2.grid(row=2, column=1)


        elif player == "paper":
            if computer == "scissor":
                gllabel3 = Label(masterrock, text="You lose")
                gllabel3.grid(row=2, column=1)

            else:
                gllabel4 = Label(masterrock, text="You win!")
                gllabel4.grid(row=2, column=1)

        elif player == "scissor":

            if computer == "rock":

                gllabel5 = Label(masterrock, text="You lose")
                gllabel5.grid(row=2, column=1)

            else:
                gllabel6 = Label(masterrock, text="You win!")
                gllabel6.grid(row=2, column=1)

    time.sleep(0.2)

mainloop()

我只有14岁,所以我的代码可能有点马虎。三苯氧胺


0条回答
网友
1楼 ·

试试这个:

from tkinter import *
from random import randint
from tkinter import messagebox
import time

options = ["rock", "paper", "scissor"]

def rock():
    global computer, masterrock, player_entry, ok_button
    masterrock = Tk()

    options_label = Label(masterrock, text="rock, paper or scissor")
    options_label.grid(row=0, column=1)

    ok_button = Button(masterrock, text="OK", command=rock2)
    ok_button.grid(row=2, column=1)

    player_entry = Entry(masterrock)
    player_entry.grid(row=1, column=1)

    computer = options[randint(0, 2)]

def rock2():
    player = player_entry.get()
    if player in options:
        player_entry.config(bg="white")
    else:
        player_entry.config(bg="red")
        return None # Stop if the user input was invalid
    ok_button.destroy()
    if player == computer:
        gllabel = Label(masterrock, text="tie")
        gllabel.grid(row=2, column=1)

    elif player == "rock":
        if computer == "paper":
            gllabel1 = Label(masterrock, text="you lose!")
            gllabel1.grid(row=2, column=1)
        else:
            gllabel2 = Label(masterrock, text="You win!")
            gllabel2.grid(row=2, column=1)

    elif player == "paper":
        if computer == "scissor":
            gllabel3 = Label(masterrock, text="You lose")
            gllabel3.grid(row=2, column=1)
        else:
            gllabel4 = Label(masterrock, text="You win!")
            gllabel4.grid(row=2, column=1)

    elif player == "scissor":
        if computer == "rock":
            gllabel5 = Label(masterrock, text="You lose")
            gllabel5.grid(row=2, column=1)

        else:
            gllabel6 = Label(masterrock, text="You win!")
            gllabel6.grid(row=2, column=1)


global masterrock
rock()
masterrock.mainloop()

您的代码有一些地方出错

首先,直接调用mainloop()不是一种好的做法。最好使用<tkinter.Tk>.mainloop()

另外,在您的代码中player1.get()player1 = Entry(masterrock)之后运行,因此用户没有足够的时间实际编写响应

您从未考虑过用户给您错误的输入,如"#"。永远不要相信用户的输入。总是检查它是否是你期望的东西

实际上你不需要一个循环<tkinter.Tk>.mainloop()是一个循环。有关更多信息,请阅读this