Python Kivy将按钮文本传递到功能?
我正在尝试创建一个简单的计算器。下面是我的代码:Python Kivy将按钮文本传递到功能?,python,kivy,Python,Kivy,我正在尝试创建一个简单的计算器。下面是我的代码: from kivy.app import App from kivy.uix.floatlayout import FloatLayout from kivy.uix.button import Button class MyApp(App): def build(self): displays = ["(", ")", "C", "<-", "7", "8", "9", " / ", "4", "5", "6"
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
class MyApp(App):
def build(self):
displays = ["(", ")", "C", "<-", "7", "8", "9", " / ", "4", "5", "6", " * ", "1", "2", "3", " - ", ".", "0",
"=", " + "]
iterator = 0
layout = FloatLayout()
for row in range(5):
for column in range(4):
posx = column * .25
posy = .75 - (row * .18)
button = Button(text=displays[iterator], pos_hint={"x": posx, "y": posy}, size_hint=(.25, .18),
color=(1, 0, 1, 1), background_color=(0, 0, 0, 1), outline_color=(1, 0, 1, 1))
layout.add_widget(button)
button.bind(on_press=lambda text=button.text: self.press(button_text=text))
iterator += 1
return layout
def press(self, button_text):
print("Called! ", button_text)
MyApp().run()
它不是传递按钮的文本,而是传递调用!0x0CAD6D88处的kivy.uix.button.button对象(内部打开-关闭[小于大于括号HTML括号?],但在此处会导致格式问题)
根据我有限的经验,似乎我传递的是按钮对象本身,而不是预期的文本。这是我第一次尝试使用Kivy,救命啊?谢谢。您使用的lambda
不正确,应该是
button.bind(on_press=lambda button: self.press(button.text))
但是,您不需要使用lambda
函数,只需获取传递给回调的实例的text
属性即可:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
class MyApp(App):
def build(self):
displays = ["(", ")", "C", "<-", "7", "8", "9", " / ", "4", "5", "6", " * ", "1", "2", "3", " - ", ".", "0",
"=", " + "]
iterator = 0
layout = FloatLayout()
for row in range(5):
for column in range(4):
posx = column * .25
posy = .75 - (row * .18)
button = Button(text=displays[iterator], pos_hint={"x": posx, "y": posy}, size_hint=(.25, .18),
color=(1, 0, 1, 1), background_color=(0, 0, 0, 1), outline_color=(1, 0, 1, 1))
layout.add_widget(button)
button.bind(on_press=self.press)
iterator += 1
return layout
def press(self, button):
print("Called! ", button.text)
MyApp().run()
从kivy.app导入应用
从kivy.uix.floatlayout导入floatlayout
从kivy.uix.button导入按钮
类别MyApp(应用程序):
def生成(自):
displays=[“(“,”),“C”,“哦!这是我尝试使用lambda的少数几次之一,我从tkinter版本中提取了大部分代码。我可以问一下press方法如何知道调用了哪个按钮吗?看起来kivy不管怎样都会传递button对象。谢谢
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
class MyApp(App):
def build(self):
displays = ["(", ")", "C", "<-", "7", "8", "9", " / ", "4", "5", "6", " * ", "1", "2", "3", " - ", ".", "0",
"=", " + "]
iterator = 0
layout = FloatLayout()
for row in range(5):
for column in range(4):
posx = column * .25
posy = .75 - (row * .18)
button = Button(text=displays[iterator], pos_hint={"x": posx, "y": posy}, size_hint=(.25, .18),
color=(1, 0, 1, 1), background_color=(0, 0, 0, 1), outline_color=(1, 0, 1, 1))
layout.add_widget(button)
button.bind(on_press=self.press)
iterator += 1
return layout
def press(self, button):
print("Called! ", button.text)
MyApp().run()