gpt4 book ai didi

Python/kivy - 类型错误 : 'kivy.properties.ObjectProperty' object is not iterable

转载 作者:太空宇宙 更新时间:2023-11-03 20:44:27 25 4
gpt4 key购买 nike

所以我一直在为我的项目制作一个骰子滚轮,我称之为:地下城主的助手帮助我DM,但现在让我们谈谈我的问题。

当我按下 RollDice 按钮时,它应该接受两个文本输入(骰子的类型和数量)并掷骰子,然后立即在控制台中打印骰子的数量和类型以及结果。然而,这反而使程序崩溃。

这是相关的Python代码(如果需要可以显示更多):

class DiceRoller(Screen):

def rolldice(self):
self.diceNumber = ObjectProperty()
self.diceType = ObjectProperty()


print(F"{self.diceNumber}D{self.diceType}")


for x in self.diceNumber:
x = x + 1

ActualDie = random.randint(1, self.diceType)
Total = Total + ActualDie

print(Total)

这是调用上面函数的按钮:

Button:
id: RollDice
text: "Roll dice"
pos_hint: {"top":0.3,"x":0}
size_hint: 0.4, 0.2
background_color: 0,0,0,0.1
color: 0,0,0,1
on_release:
root.rolldice()

正如我所说并表明应该发生的事情是,它应该打印例如 6D6,然后打印这些骰子的总数。然而,这就是发生的情况(错误代码):

enter image description here

该值由同屏的两个文本输入框设置:kv文件中的链接。

Screen - ObjectProperty links to TextInput

最佳答案

您使用的属性是错误的,它们必须在类级别声明:

class DiceRoller(Screen):
diceNumber = ObjectProperty()
diceType = ObjectProperty()

def rolldice(self):
print(F"{self.diceNumber}D{self.diceType}")
# etc.

在按照您的方式访问它们之前,您还需要将它们设置为实际值,但我认为您未显示的代码已涵盖了这一点。

关于Python/kivy - 类型错误 : 'kivy.properties.ObjectProperty' object is not iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56691348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com