Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter:循环计算机';让我们转几圈_Python_User Interface_Tkinter_Tk_Dice - Fatal编程技术网

Python Tkinter:循环计算机';让我们转几圈

Python Tkinter:循环计算机';让我们转几圈,python,user-interface,tkinter,tk,dice,Python,User Interface,Tkinter,Tk,Dice,我正在为骰子游戏(猪)编写一个程序。在游戏中,玩家将掷d6,直到他们决定保留分数(传给计算机)或掷1,这将自动使计算机轮到他们 我遇到的问题是,我需要的功能是让计算机循环十次。我希望计算机将模具滚动十次,它可以滚动一次并传递回玩家,也可以在十次滚动后保持不变。我如何让计算机在不使用Tk内部循环的情况下将模具滚动十次 代码如下: from Tkinter import * from random import * class App(Tk): def __init__(self):

我正在为骰子游戏(猪)编写一个程序。在游戏中,玩家将掷d6,直到他们决定保留分数(传给计算机)或掷1,这将自动使计算机轮到他们

我遇到的问题是,我需要的功能是让计算机循环十次。我希望计算机将模具滚动十次,它可以滚动一次并传递回玩家,也可以在十次滚动后保持不变。我如何让计算机在不使用Tk内部循环的情况下将模具滚动十次

代码如下:

from Tkinter import *
from random import *

class App(Tk):
    def __init__(self):
        Tk.__init__(self)

        self.headerFont = ("courier new", "16", "bold")

        self.title("Pig, The Dice Game")
        self.headers()
        self.rollDie()

    def headers(self):
        Label(self, text = "Instructions", font = self.headerFont).grid(columnspan = 4)
        Label(self, text = "Text", font = self.headerFont).grid(row = 1, columnspan = 4)

        Label(self).grid(row = 1, columnspan = 4)
        Label(self, text = "The Game of Pig", font = self.headerFont).grid(row = 2, columnspan = 4)

    def rollDie(self):
        self.btnRoll = Button(self, text = "Roll The Die")
        self.btnRoll["state"] = 'active'
        self.btnRoll.grid(row = 3, columnspan = 4)
        self.btnRoll["command"] = self.playerTurn

        self.btnHold = Button(self, text = "Hold")
        self.btnHold["state"]= 'active'
        self.btnHold.grid(row = 4, columnspan = 4)
        self.btnHold["command"] = self.compTurn

        self.btnPass = Button(self, text = "Pass")
        self.btnPass.grid(row = 5, columnspan = 4)
        self.btnPass["command"] = self.compTurn

        Label(self, text = "You Rolled:").grid(row = 6, column = 0)
        self.lblYouRolled = Label(self, bg = "#fff", anchor = "w", relief = "groove")
        self.lblYouRolled.grid(row = 6, column = 1, columnspan = 1, sticky = "we")

        Label(self, text = "Options:").grid(row = 7, column = 0)
        self.lblOptions = Label(self, bg = "#fff", anchor = "w", relief = "groove")
        self.lblOptions.grid(row = 7, column = 1, sticky = "we")

        Label(self, text = "Player One Turn Score:").grid(row = 8, column = 0)
        self.lblPlayerOneTurnScore = Label(self, bg = "#fff", anchor = "w", relief = "groove")
        self.lblPlayerOneTurnScore.grid(row = 8, column = 1, sticky = "we")


    def playerTurn(self):
        self.oneTurnTotal = [0]
        self.oneRoll = randint(1,6)
        self.btnHold["state"] = 'active'

        self.lblYouRolled["text"] = self.oneRoll

        if self.oneRoll != 1:
            self.oneTurnTotal.append(self.oneRoll)
            self.lblOptions["text"] = "Roll again, or hold and pass the dice to Player Two."
        else:
            self.lblOptions["text"] = "You rolled a 1! Click 'Pass' to pass your turn to the computer."
            self.oneTurnTotal = [0]
            self.btnRoll["state"] = 'disabled'
            self.btnHold["state"] = 'disabled'


    def calculatePlayerOneTurnScore(self):
        turnScore = sum(self.oneTurnTotal)
        self.lblPlayerOneTurnScore["text"] = turnScore



    def compTurn(self):

        self.compTurnTotal = [0]
        self.compRoll = randint(1,6)

        self.lblYouRolled["text"] = self.compRoll

        if self.compRoll != 1:
            self.compTurnTotal.append(self.compRoll)
            self.lblOptions["text"] = "The computer will roll again."

        else:
            self.lblOptions["text"] = "The computer rolled a 1! Its turn has ended."
            self.compTurnTotal = [0]
            self.btnRoll["state"] = 'active'

    def calculatePlayerTwoTurnScore(self):
        turnScore = sum(self.twoTurnTotal)
        self.lblPlayerTwoTurnScore["text"] = turnScore


def main():
  app = App()
  app.mainloop()

if __name__ == "__main__":
  main()

您可以将骰子滚动作为顶级小部件,然后让您的根“wait_window”等待滚动小部件完成。在toplevel小部件中创建一个函数,生成点名并调用它十次

范例

from Tkinter import *

yourscorevalue=12
cpuscorevalue=10

class dice:
    def __init__(self,parent):
        rollcount=0
        top=self.top=Toplevel(parent)
        while 1:
            roll=self.diceroll()
            rollcount+=1
            if rollcount==10 or roll==1:
                break

    def diceroll(self):
        #Random Dice Roll code

class dicegame:
    def __init__(self):
        self.root=Tk()
        self.root.title('Dicerolling Game')
        Label(self.root, text='Your Score').grid(column=0,row=0,sticky='ew')
        Label(self.root, text=yourscorevalue, background='black',foreground='red').grid(column=1,row=0,sticky='ew')
        Label(self.root, text='Computer Score').grid(column=0,row=1,sticky='ew')
        Label(self.root, text=cpuscorevalue, background='black',foreground='red').grid(column=1,row=1,sticky='ew')
        b=Button(self.root, text='Roll The Dice', command=self.roll).grid(column=0,row=2,sticky='ew')
        c=Button(self.root, text="Done", command=self.root.destroy).grid(column=1,row=2,sticky='ew')

        mainloop()

    def roll(self):
        d=dice(self.root)
        self.root.wait_window(d.top)

“在Tk内部不使用循环”是什么意思?使用循环有什么错?一个只做一点数学运算的短循环是完全无害的。你是否希望计算机滚动一次,暂停一秒钟,让用户知道发生了什么,再次滚动,暂停,滚动,暂停?或者,你想让计算机模拟多达10个转鼓,然后返回结果吗?我被告知,你真的不应该在Tk中使用循环,因为主函数已经是一个循环了。我如何让计算机的功能重复,直到它滚动一次或十次?模拟十个转鼓并返回结果就可以了。在Tk中使用一个循环没有什么错。实际上,如果掷骰子在顶级窗口中,并且主循环将暂停,直到掷骰完成,则可以调用wait_window()。