tkinter按钮不调用函数

时间:2016-07-07 04:16:19

标签: python tkinter

我是初学者程序员,试图创建一个程序,允许用户使用tkinter播放Rock,Paper,Scissors。我没有在课堂上学过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()

1 个答案:

答案 0 :(得分:0)

显示不会更改,因为要更改您需要执行的标签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")

相关问题