Question

For my final project in an intro course to Python, I've got to create a game of Pig (the dice game) using a GUI.

I'm try to create a single die roll and record that information so that I can retrieve it and calculate the turn score and the total score. The problem is, I can't figure out how to store the die roll information and retrieve it so I can make those calculations. As it stands, I'm storing the information within the label itself and trying to retrieve it from there, but I can't do calculations unless it's an integer. Labels, from what I'm to understand, work only with text and images.

Here's the code:

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()
Was it helpful?

Solution

You are getting your roll information with self.roll = randint(1,6). You can append it to a list. Then use it as you like.

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

Since I don't know how the game works, this is all I can help.
By the way, labels can show integers also. So your line works fine if you add self..

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

EDIT:

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)

I ran above code twice and these are the outputs.

>>> Roll: 2  Total sum: 8

>>> Roll: 4  Total sum: 10
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top