tkinter不调用函数的按钮
问题描述:
我是一个初学程序员,试图创建一个程序,允许用户使用tkinter的玩家Rock,Paper,Scissors。我没有在课堂上学过tkinter,但这是我最后的项目,所以我试图学习新的东西来打动人心。tkinter不调用函数的按钮
我能弄清楚如何一切的位置,但我的按钮似乎并没有做任何事情。退出按钮有效,但不是其他任何东西。没有解释器错误出现。任何帮助将不胜感激。
from tkinter import *
from tkinter import ttk
import random
def get_computer_hand(): #Picks computer's hand.
pick = random.randint(1,3) #Randomly chooses a number between 1 and 3.
if pick == 1:
hand = "Rock"
elif pick == 2:
hand = "Paper"
elif pick == 3:
hand = "Scissors"
return hand
def setRock():
global playerHand
global computerHand
playerHand = "Rock"
computerHand = get_computer_hand()
runGame(playerHand,computerHand)
def setPaper():
global playerHand
global computerHand
playerHand = "Paper"
computerHand = get_computer_hand()
runGame(playerHand,computerHand)
def setScissors():
global playerHand
global computerHand
playerHand = "Scissors"
computerHand = get_computer_hand()
runGame(playerHand,computerHand)
def setQuit():
root.destroy()
def runGame(player,computer):
global playerWins
global computerWins
global draws
global win_or_loss
playerHand = player
computerHand = computer
if(player == "Rock"):
if(computer == "Paper"):
computerWins += 1
win_or_loss = "Paper beats Rock. You lose!"
elif(computer == "Scissors"):
playerWins += 1
win_or_loss = "Rock beats Scissors. You win!"
else:
draws += 1
win_or_loss = "Draw!"
if(player == "Paper"):
if(computer == "Rock"):
playerWins += 1
win_or_loss = "Paper beats Rock. You win!"
elif(computer == "Scissors"):
computerWins += 1
win_or_loss = "Paper beats Rock. You win!"
else:
draws += 1
win_or_loss = "Draw!"
if(player == "Scissors"):
if(computer == "Rock"):
computerWins += 1
win_or_loss = "Rock beats Scissors. You lose!"
elif(computer == "Paper"):
playerWins += 1
win_or_loss = "Scissors beats Paper. You win!"
else:
draws += 1
win_or_loss = "Draw!"
root = Tk()
root.title("Rock Paper Scissors")
playerWins = 0
computerWins = 0
draws = 0
playerHand = ""
computerHand = ""
win_or_loss = ""
mainFrame = ttk.Frame(root, padding="3 3 12 12")
mainFrame.grid(column=0, row=0, sticky=(N, W, E, S))
mainFrame.columnconfigure(0, weight=1)
mainFrame.rowconfigure(0, weight=1)
buttonFrame = ttk.Frame(mainFrame)
handFrame = ttk.Frame(mainFrame)
ttk.Label(mainFrame, text="Let's play Rock, Paper, Scissors. Ready?").grid(column=0,row=0)
rockButton = ttk.Button(buttonFrame, text="Rock", command = setRock)
paperButton = ttk.Button(buttonFrame, text="Paper", command = setPaper)
scissorsButton = ttk.Button(buttonFrame, text="Scissors", command = setScissors)
quitButton = ttk.Button(buttonFrame, text="Quit", command = setQuit)
rockButton.pack(side="left",fill=None,expand=False)
paperButton.pack(side="left",fill=None,expand=False)
scissorsButton.pack(side="left",fill=None,expand=False)
quitButton.pack(side="left",fill=None,expand=False)
buttonFrame.grid(column=0,row=1)
playerHandLabel = ttk.Label(handFrame, text="You picked: " + playerHand)
computerHandLabel = ttk.Label(handFrame, text="Computer picked: " + computerHand)
playerHandLabel.pack(side="left",fill=None,expand=False)
computerHandLabel.pack(side="left",fill=None,expand=False)
handFrame.grid(column=0,row=2)
winOrLoss = ttk.Label(mainFrame, text=win_or_loss).grid(column=0,row=3)
ttk.Label(mainFrame, text = "Player Wins: " + str(playerWins)).grid(column=0,row=4)
ttk.Label(mainFrame, text = "Computer Wins: " + str(computerWins)).grid(column=0,row=5)
ttk.Label(mainFrame, text = "Draws: " + str(draws)).grid(column=0,row=6)
for child in mainFrame.winfo_children(): child.grid_configure(padx=5, pady=5)
root.mainloop()
答
显示不改变,因为改变标签的文本winOrLoss
你需要做的:winOrLoss.configure(text=win_or_loss)
,只是改变win_or_loss
不会更改标签的内容。
另一种可能性是使用STRINGVAR:
win_or_loss = StringVar()
winOrLoss = Label(textvariable=win_or_loss)
,然后,改变标签的内容,你做win_or_loss.set("new string")
。
按钮确实有用,它们只是不会给你任何视觉反馈。他们设置'playerWins'和'win_or_loss'变量,但它们不更新GUI。 –