【问题标题】:Python Tk: Can't Retrieve Dice Roll Integer for CalculationsPython Tk:无法检索掷骰子整数进行计算
【发布时间】: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()

【问题讨论】:

    标签: python tkinter tk dice


    【解决方案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
    

    【讨论】:

    • 这很有道理,我认为这是正确的方法,但我收到一个错误:"AttributeError: 'list' object attribute 'append' is read-only"
    • 不要使用 list 作为变量名。试试别的,因为 list 是一个内置类。
    • 哦,我不是,我实际上使用了与您相同的名称,因为它们有意义。
    • 你对这段代码做了什么?因为这对我来说很好。
    • 你的 Tk 答案一如既往的好:p
    猜你喜欢
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 2021-03-12
    • 2022-01-21
    • 1970-01-01
    相关资源
    最近更新 更多