Python Tk:无法为计算检索Dice Roll Integer

时间:2014-04-27 17:55:35

标签: python tkinter tk dice

对于我在Python入门课程中的最后一个项目,我必须使用GUI创建一个Pig游戏(骰子游戏)。

我尝试创建单个掷骰子并记录该信息,以便我可以检索它并计算转弯得分和总得分。问题是,我无法弄清楚如何存储模具卷信息并检索它,以便我可以进行这些计算。就目前而言,我将信息存储在标签本身并尝试从那里检索,但除非它是一个整数,否则我无法进行计算。根据我的理解,标签仅适用于文字和图像。

以下是代码:

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.playerTurn()
        self.getTurnScore()

    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 playerTurn(self):
        self.btnRoll = Button(self, text = "Roll The Die")
        self.btnRoll.grid(row = 3, columnspan = 2)
        self.btnRoll["command"] = self.calculateRoll

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

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

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

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


    def calculateRoll(self):
        self.roll = randint(1,6)

        #self.lblYouRolled["text"] = roll

    def getTurnScore(self):
        #self.lblTurnScore["text"] =


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

if __name__ == "__main__":
  main()

1 个答案:

答案 0 :(得分:1)

您正在使用self.roll = randint(1,6)获取您的论坛信息。您可以将其附加到列表中。然后随意使用它。

self.all_rolls = [] #if you dont want to get an empty list each button click, you might want to make this definition under __init__
self.roll = randint(1,6)
self.all_rolls.append(self.roll)
#to use your rolls, you can iterate over that list
for item in self.all_rolls:
   print (item)  #this is just for an example ofcourse

由于我不知道游戏是如何运作的,所以我可以提供帮助 顺便说一句,标签也可以显示整数。因此,如果添加self.,您的行就可以了。

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

修改

from random import *
summ = 0
all_rolls = [1,2,3]
roll = randint(1,6)
all_rolls.append(roll)

for die_roll in all_rolls:
    summ += die_roll

print ("Roll:",roll," Total sum",summ)

我在上面运行了两次代码,这些是输出。

>>> Roll: 2  Total sum: 8

>>> Roll: 4  Total sum: 10