首页 > 解决方案 > 单击时未执行 Tkinter 命令按钮且没有错误消息-(不是 () 或缩进问题)-

问题描述

crapsgui.py - 成为两个骰子滚动游戏脚本的一部分

这是一个骰子游戏,限于篇幅,这里只附上GUI部分的脚本!

“New Game”按钮不执行“newGame”功能,调试器只停在

def newGame(self)

这里相关的三行是:

按钮设置线:-

self.newButton = self.addButton(row=4, column=1, text="New Game", command=self.newGame)

函数声明行 -

def newGame(self):

保持按钮随时待命的循环指令 -

CrapsGUI().mainloop()

在我添加要求用户输入“游戏编号”的部分之前,函数 newGame(self) 运行良好,但在 EasyFrame 中添加更多行后,按钮变得无响应!我是 python 新手,花了很多天时间试图修复它!会是最

from breezypythongui import EasyFrame
from tkinter import PhotoImage
from craps2 import Player

class Var:
def __init__(self, wins=0, losses=0, winRolls=0, lossRolls=0, number=0, Res="" ):
    self.wins = wins
    self.losses = losses
    self.winRolls = winRolls
    self.lossRolls = lossRolls
    self.number = number
    self.Res = Res   
    # these variable for control when to stop the game & calculating the roll results statistics after all games completed

p1=Var()

class CrapsGUI(EasyFrame): #set up GUI frame with two buttons
    def __init__(self):
        EasyFrame.__init__(self, title="Craps Game")
        self.newGame = None
        self.setSize(200, 200)
        self.player = Player()
        self.v1 = 1
        self.v2 = 2
        self.dieLabel1 = self.addLabel("", row=0, column=0, sticky="NSEW") # Dice image 1
        self.dieLabel2 = self.addLabel("", row=0, column=1, sticky="NSEW")  # Dice image 2

        self.StartArea1= self.addTextArea("Enter the number of games:  ", row=1, column=0, width=7, 
        height=1)  #Ask user to input the number of game to play
        self.inputField = self.addIntegerField(value=0, row=1, column=1)
        self.addButton(text="Enter", row=1, column=2, columnspan=2, command=self.enter)

        self.stateArea = self.addTextArea("", row=3, column=0, columnspan=2, width=10, height=5)

        self.rollButton = self.addButton(row=4, column=0, text="Roll", command=self.nextRoll)
        self.newButton = self.addButton(row=4, column=1, text="New Game", command=self.newGame)

        self.resultArea = self.addTextArea("", row=5, column=0, columnspan=2, width=17, height=7)

        self.refreshImages()

    def enter(self):    #this button to submit the inputted number of dice game to play, "number"
        p1.number = self.inputField.getNumber()
        self.outputField.setNumber(p1.number)

    def nextRoll(self):   # function to roll the dices once and display the rolling result
        self.stateArea = self.addTextArea("", row=3, column=0, columnspan=2, width=10, height=5)
        (self.v1, self.v2) = self.player.rollDice()
        total = self.v1+self.v2
        p1.Res = p1.Res + "Total = "+ str(total) + " - roll no.:"+ str(self.player.rollsCount)+ "\n"
        self.stateArea.appendText(str(Res))
        self.refreshImages()
        if self.player.isWinner():
            p1.wins += 1
            p1.winRolls += self.player.rollsCount
            self.stateArea.appendText("You won!" + " " + str(p1.wins) + ",  " + str(p1.wins + p1.losses)+ ",  
            "+str(p1.number))
            self.rollButton["state"] = "disabled"
        elif self.player.isLoser():
            p1.losses += 1
            p1.lossRolls += self.player.rollsCount
            self.stateArea.appendText("You lose!"+ " " + str(p1.losses) + ",  " + str(p1.wins + p1.losses) + 
            ",  "+str(p1.number))
            self.rollButton["state"] = "disabled"

        if (p1.wins + p1.losses) == number:  #when number of wins plus losses = number, disable Roll button
            self.resultArea.appendText("The total number of wins is " +str(p1.wins) +"\n")
            self.resultArea.appendText("The total number of losses is " +str(p1.losses) +"\n" )
            if wins > 0:
            self.resultArea.appendText("The average number of rolls per win is " +str(p1.winRolls/p1.wins) 
            +"\n")
            if p1.losses > 0:
                self.resultArea.appendText("The average number of rolls per loss is " 
                +str(p1.lossRolls/p1.losses) +"\n")
                self.resultArea.appendText("The winning percentage is " +str(p1.wins/p1.number) )
            else:
                pass

    def newGame(self):  #button to start another game
        Res=""
        if (p1.wins + p1.losses) < number:
            self.player = Player()
            self.v1 = 1
            self.v2 = 1
            self.stateArea.setText("")
            self.refreshImages()
            self.rollButton["state"] = "normal"
        else:
            self.rollButton["state"] = "disabled"

    def refreshImages(self):   #refresh the images of two dices in use
        fileName1 = "DICE/"+str(self.v1)+".gif"
        fileName2 = "DICE/"+str(self.v2)+".gif"
        self.image1 = PhotoImage(file=fileName1)
        self.dieLabel1["image"] = self.image1
        self.image2 = PhotoImage(file=fileName2)
        self.dieLabel2["image"] = self.image2

def main():
    CrapsGUI().mainloop()   #loop to make the two command button on call 

if __name__ == "__main__":
    main()

标签: pythonuser-interfacetkintercommand

解决方案


问题是

self.newGame = None 

取代

def newGame(self) 

最后你command=None在创建时分配self.newButton.

您必须self.newGame = None为此变量删除或使用不同的名称。


推荐阅读