tkinter python 3.8.2中函数更新标签的问题

2024-09-19 23:39:03 发布

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

所以我一直在教自己一些tkinter来开始构建一些真正的应用程序,我的第一个项目是为Courera非常著名的石头剪刀蜥蜴Spock游戏构建一个界面

到目前为止,我所有的按钮都工作得很好(尽管我觉得如果我反复点击同一个按钮而不改变选择,它们不会更新任何内容,因为匹配的结果永远不会改变),结果面板也工作得很好。这正是让我发疯的原因,据我所知,赢家计数器和计算机选择面板遵循相同的逻辑,出于某种原因,当我单击按钮时,它们不会更新。有什么提示吗

感谢您的耐心,代码如下

import tkinter as tk
import random
from functools import partial

#setting the window early as I had issues and bugs when called after defining functions
window = tk.Tk()
window.geometry('350x200')

#global variables
result= tk.StringVar()
result.set("")
comp = tk.IntVar()
guess = tk.StringVar()
guess.set("")
playerWin = tk.IntVar()
playerWin.set(0)
compWin = tk.IntVar()
compWin.set(0)

#function that handles the computer's play in each game
def compPlay():
    global guess, comp
    comp.set(random.randrange(0,5))
    if comp.get()== 0:
        guess.set("Rock")
    elif comp.get()== 1:
        guess.set("Spock")
    elif comp.get() == 2:
        guess.set("Paper")
    elif comp.get() == 3:
        guess.set("Lizard")
    elif comp.get() == 4:
        guess.set("Scissors")

#function to play human vs computer choices and see who wins
def gameplay(playerNum,compNum):
    global result, comp, playerWin, compWin
    if playerNum == comp.get():
        result.set("It's a tie!")
    elif (playerNum - comp.get()) % 5 <= 2:
        result.set("Player wins!")
        playerWin = playerWin.get() + 1
    elif (playerNum - comp.get()) % 5 >= 3:
        result.set("Computer wins!")
        compWin += compWin.get() + 1
    else:
        result.set(text = "")
        
# game title
lblGame= tk.Label(text="Rock, Scissors, Paper, Lizard, Spock").pack()

#frame with the buttons for player choices
playerFrame = tk.Frame(window)
btRock = tk.Button(playerFrame, text = "Rock", width = 15, command = partial(gameplay, 0,compPlay)).pack()
btScissors = tk.Button(playerFrame, text = "Scissors", width = 15, command = partial(gameplay, 1,compPlay)).pack()
btPaper = tk.Button(playerFrame, text = "Paper", width = 15, command = partial(gameplay, 2,compPlay)).pack()
btLizard = tk.Button(playerFrame, text = "Lizard", width = 15, command = partial(gameplay, 3,compPlay)).pack()
btSpock = tk.Button(playerFrame, text = "Spock", width = 15, command = partial(gameplay, 4,compPlay)).pack()
playerFrame.pack(side = tk.LEFT)

#frame with info about the game, as in what the computer chose and the result of the play
compFrame = tk.Frame(window)
lbComp = tk.Label(compFrame, text = "Computer plays:").pack()
lbGuess = tk.Label(compFrame, textvariable = guess, relief = tk.GROOVE, borderwidth = 5, width = 15).pack()
lbRes = tk.Label(compFrame, text = "and the result of the game is").pack()
lbMatch = tk.Label(compFrame, textvariable = result, relief = tk.GROOVE, borderwidth = 5, width = 15).pack()

#mini frames for score keeping
playerFrame = tk.Frame(compFrame, relief = tk.GROOVE, borderwidth = 3)
playerSide = tk.Label(playerFrame, text = "Player points:").pack()
playerScore = tk.Label(playerFrame, textvariable = str(playerWin)).pack()
playerFrame.pack(side = tk.LEFT)

compScoreFrame = tk.Frame(compFrame, relief = tk.GROOVE, borderwidth = 3)
compSide = tk.Label(compScoreFrame, text = "Computer points:").pack()
compScore = tk.Label(compScoreFrame, textvariable = str(compWin)).pack()
compScoreFrame.pack(side = tk.RIGHT)

compFrame.pack(side = tk.RIGHT)

window.mainloop()

每当游戏给任何一个玩家打分时,我都会在控制台上看到这个错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
    return self.func(*args)
  File "~/Interactive Python/teste tkinter3.py", line 54, in gameplay
    playerWin = playerWin.get() + 1
AttributeError: 'int' object has no attribute 'get'

Tags: thetextgetresultwidthlabelpacktk
2条回答

这里有几个问题不能正常工作:

  1. 计算机播放标签字段不会刷新,因为您从未调用compPlay()函数。每次播放器按下左侧按钮时都应调用此函数,但在gameplay方法中未使用此函数。只需调用此函数即可刷新计算机并设置标签的值
  2. gameplay函数中compWinplayerWin对象不是ints而是tkinter.Intvar,因此您应该set它们的变量,而不是使用++=。这就是这个错误的原因

这是因为playWin是一个tkinter.Intvar对象,您需要将函数gameplay更改为:

def gameplay(playerNum, compNum):
    global result, comp, playerWin, compWin
    if playerNum == comp.get():
        result.set("It's a tie!")
    elif (playerNum - comp.get()) % 5 <= 2:
        result.set("Player wins!")
        playerWin.set(playerWin.get() + 1)
    elif (playerNum - comp.get()) % 5 >= 3:
        result.set("Computer wins!")
        compWin.set(compWin.get() + 1)
    else:
        result.set(text="")

相关问题 更多 >