我正在尝试通过ID禁用按钮。据我了解,发送ID(Pong)不起作用。但是,对于我的最终应用程序,无论如何,我需要发送字母A,B,... F,那么我如何可以建立一个小部件ID的字符串并将其禁用呢?我必须将字符串转换为另一种数据类型吗?我的.py
# main.py
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
class TurboWidget(GridLayout):
def ping(self, y):
x = "btnStart"+y
print(y)
print(x)
self.ids.x.disabled = True
def pong(self, y):
print(y)
self.ids.y.disabled = True
class TurboApp(App):
def build(self):
return TurboWidget()
if __name__ == "__main__":
TurboApp().run()
我的.kv
# turbo.kv
<TurboWidget>
cols: 2
Button:
id: btnStartA
text:"A"
on_release: root.Ping("A")
Button:
id: btnStartB
text:"B"
on_release: root.Ping("B")
Button:
id: btnStartC
text:"C"
on_release: root.Pong(btnStartC.id)
Button:
id: btnStartD
text:"D"
on_release: root.Pong(btnStartD.id)
答案 0 :(得分:1)
您可以使用以下方法执行此操作。请注意,我将id作为字符串传递给方法内部。另外,我正在使用self.ids[y].disabled
从python端设置kivy属性。
# main.py
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
class TurboWidget(GridLayout):
def ping(self, y):
self.ids[y].disabled = True
def pong(self, y):
print(y)
self.ids[y].disabled = True
class TurboApp(App):
def build(self):
return TurboWidget()
if __name__ == "__main__":
TurboApp().run()
,并在与main.py相同的文件夹中使用以下kv文件。
# turbo.kv
<TurboWidget>
cols: 2
Button:
id: btnStartA
text:"A"
on_press: root.ping("btnStartA")
Button:
id: btnStartB
text:"B"
on_press: root.ping("btnStartB")
Button:
id: btnStartC
text:"C"
on_press: root.pong("btnStartC")
Button:
id: btnStartD
text:"D"
on_press: root.pong("btnStartD")
答案 1 :(得分:0)
self.ids.x.disabled= True
需要成为
self.ids[x].disabled= True
没有点。