【发布时间】:2014-04-29 16:00:14
【问题描述】:
对于 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()
【问题讨论】: