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()