0
我是一個初學程序員,試圖創建一個程序,允許用戶使用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()
按鈕確實有用,它們只是不會給你任何視覺反饋。他們設置'playerWins'和'win_or_loss'變量,但它們不更新GUI。 –