Python/Kivy新手,尝试制作一个简单的签出系统
我的compsci课程有一个作业,是制作一个简单的GUI来检查产品。我们上周才开始使用Python/Kivy。我有一个问题,让我的按钮添加到一个总的价格 我想让这个程序做的是,当你点击一个免费汉堡按钮时,加上2美元。当你点击付费汉堡的按钮时,加上1美元。我很难把它们加到总数中去Python/Kivy新手,尝试制作一个简单的签出系统,python,kivy,Python,Kivy,我的compsci课程有一个作业,是制作一个简单的GUI来检查产品。我们上周才开始使用Python/Kivy。我有一个问题,让我的按钮添加到一个总的价格 我想让这个程序做的是,当你点击一个免费汉堡按钮时,加上2美元。当你点击付费汉堡的按钮时,加上1美元。我很难把它们加到总数中去 from kivy.app import App from kivy.modules import inspector # For inspection. from kivy.core.window import Win
from kivy.app import App
from kivy.modules import inspector # For inspection.
from kivy.core.window import Window # For inspection.
from kivy.properties import NumericProperty
from kivy.properties import BooleanProperty
__app_package__ = 'edu.unl.cse.soft161.order'
__app__ = 'Order Meal'
__version__ = '0.1'
__flags__ = ['--bootstrap=sdl2', '--requirements=python2,kivy', '--orientation=landscape']
class OrderApp(App):
total = NumericProperty(0)
freeBurger = BooleanProperty(False)
paidBurger = BooleanProperty(False)
def build(self):
inspector.create_inspector(Window, self) # For inspection (press control-e to toggle).
def item(self, item):
amount = NumericProperty(self.item)
self.total = amount + self.total
def service(self, service):
amount = NumericProperty(self.service)
self.total = self.total * amount
if __name__ == '__main__':
app = OrderApp()
app.run()
KIVY应用程序在这里
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
Button:
id: button1
text: 'Order Free Burger'
value: 2
on_press: app.item(self.value)
Button:
id: button2
text: 'Order Paid Burger'
value: 1
on_press: app.item(self.value)
BoxLayout:
orientation: 'horizontal'
Button:
id: service1
value: 1.2
text: 'Good service'
on_press: app.service(self.value)
Button:
id: service2
value: 1.15
text: 'Okay service'
Button:
id: service3
value: 1.1
text: 'Bad service'
BoxLayout:
orientation: 'horizontal'
Label:
id: label1
text: 'Meal Total:'
Label:
id: totalLabel
text: str(app.total)
解决方案:
而不是你的
on_press: app.service(self.value)
在.kv文件中,您可以简单地执行以下操作
on_press: app.total += self.value
您可以安全地删除Python文件中的item方法
替代解决方案:
将.kv文件保持原样,并将item方法更改为read
def item(self, amount):
self.total = amount + self.total # or shorter: self.total += amount
解释您的问题:
在item方法的版本中,使用self.item而不是名为item的参数。但是self指的是OrderApp类型的对象,因此self.item指的是其名为item的方法,而不是该方法的参数,该方法恰好具有相同的名称。为了减少混淆的可能性,我在第二个解决方案中将参数的名称改为amount
此外,看起来好像您试图在item方法内定义NumericProperty。不要那样做;此类属性声明仅在任何方法之外的类级别进行。由于您只希望在item方法结束之前使用参数项,因此不需要永久保存它。可以将参数添加到self.total,而无需任何转换
如果您再次面临类似的问题,在代码中添加一些print语句可能会有所帮助。这允许您在应用程序崩溃之前查看变量的状态。这种技术的一个例子是
def item(self, item):
print "*" * 300 # prints 300 asterisks so you can find it quickly
print self.item # shows that self.item is a method and *not* the argument
# ... more code ...
玩得开心 我的回答解决了你的问题吗?如果没有,请告诉我。如果是,请考虑将其标记为接受。