Python : 如何设置按钮的id
Python : How to set id of button
我正在使用 python-2.7
和 kivy-1.9.0
。有人能告诉我如何设置按钮的 id
吗?
我正在尝试使用此代码设置按钮的 idtest
。
btn1 = Button(text="Close",id="test")
但是报错'Alert' object has no attribute 'test'
test.py
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
Window.size = (500, 150)
class Alert(Popup):
def __init__(self, title, text):
super(Alert, self).__init__()
box = BoxLayout(orientation='vertical', padding=(5))
box.add_widget(Label(text=text))
btn1 = Button(text="Close",id="test")
box.add_widget(btn1)
self.title = title
self.title_size = 30
self.title_align = 'center'
self.content = box
self.size_hint = (None, None)
self.size = (300, 200)
self.auto_dismiss = False
self.open()
self.test.background_color = [0, 0, 1, 0.5]
class Test(App):
def build(self):
Alert(title='yeah!', text='inputs are invalid')
return
if __name__ == '__main__':
Test().run()
如果您正在寻找 id 以在以后的情况下使用按钮,您可以通过任何一种方式使用它,
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder`enter code here`
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
Window.size = (500, 150)
class Alert(Popup):
btn1 = Button(text="Close")
def __init__(self, title,btn1, text):
super(Alert, self).__init__()
self.btn1 = btn1
box = BoxLayout(orientation='vertical', padding=(5))
box.add_widget(Label(text=text))
box.add_widget(self.btn1)
self.title = title
self.title_size = 30
self.title_align = 'center'
self.content = box
self.size_hint = (None, None)
self.size = (300, 200)
self.auto_dismiss = False
self.open()
self.test.background_color = [0, 0, 1, 0.5]
class Test(App):
def build(self):
change_button = Button(text="Close")
Alert(title='yeah!',btn1=change_button, text='inputs are invalid')
return
if __name__ == '__main__':
Test().run()
定义按钮 ID
您在 Python 脚本中将 id 分配给按钮的正确方法。
btn1 = Button(text="Close")
self.ids['test'] = btn1
备注
在Python脚本中声明的id不同于ids 在 kv 文件中定义.
错误
您遇到的错误不是因为按钮的id设置不正确。关键字 self 引用“当前小部件实例”即 Alert/Popup 并且它没有属性 test.
File ".../main.py", line 33, in __init__
self.test.background_color = [0, 0, 1, 0.5]
AttributeError: 'Alert' object has no attribute 'test'
解决方案
定义按钮时,它被分配给一个对象btn1。因此,如果你想改变按钮的背景颜色,使用
btn1.background_color or sef.ids.test.background_colour or self.ids['test'].background_color
例子
main.py
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
Window.size = (500, 150)
class Alert(Popup):
def __init__(self, title, text):
super(Alert, self).__init__()
box = BoxLayout(orientation='vertical', padding=(5))
box.add_widget(Label(text=text))
btn1 = Button(text="Close")
self.ids['test'] = btn1
box.add_widget(btn1)
self.title = title
self.title_size = 30
self.title_align = 'center'
self.content = box
self.size_hint = (None, None)
self.size = (300, 200)
self.auto_dismiss = False
self.open()
self.ids.test.background_color = [0, 0, 1, 0.5]
class Test(App):
def build(self):
Alert(title='yeah!', text='inputs are invalid')
return
if __name__ == '__main__':
Test().run()
输出
我正在使用 python-2.7
和 kivy-1.9.0
。有人能告诉我如何设置按钮的 id
吗?
我正在尝试使用此代码设置按钮的 idtest
。
btn1 = Button(text="Close",id="test")
但是报错'Alert' object has no attribute 'test'
test.py
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
Window.size = (500, 150)
class Alert(Popup):
def __init__(self, title, text):
super(Alert, self).__init__()
box = BoxLayout(orientation='vertical', padding=(5))
box.add_widget(Label(text=text))
btn1 = Button(text="Close",id="test")
box.add_widget(btn1)
self.title = title
self.title_size = 30
self.title_align = 'center'
self.content = box
self.size_hint = (None, None)
self.size = (300, 200)
self.auto_dismiss = False
self.open()
self.test.background_color = [0, 0, 1, 0.5]
class Test(App):
def build(self):
Alert(title='yeah!', text='inputs are invalid')
return
if __name__ == '__main__':
Test().run()
如果您正在寻找 id 以在以后的情况下使用按钮,您可以通过任何一种方式使用它,
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder`enter code here`
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
Window.size = (500, 150)
class Alert(Popup):
btn1 = Button(text="Close")
def __init__(self, title,btn1, text):
super(Alert, self).__init__()
self.btn1 = btn1
box = BoxLayout(orientation='vertical', padding=(5))
box.add_widget(Label(text=text))
box.add_widget(self.btn1)
self.title = title
self.title_size = 30
self.title_align = 'center'
self.content = box
self.size_hint = (None, None)
self.size = (300, 200)
self.auto_dismiss = False
self.open()
self.test.background_color = [0, 0, 1, 0.5]
class Test(App):
def build(self):
change_button = Button(text="Close")
Alert(title='yeah!',btn1=change_button, text='inputs are invalid')
return
if __name__ == '__main__':
Test().run()
定义按钮 ID
您在 Python 脚本中将 id 分配给按钮的正确方法。
btn1 = Button(text="Close")
self.ids['test'] = btn1
备注
在Python脚本中声明的id不同于ids 在 kv 文件中定义.
错误
您遇到的错误不是因为按钮的id设置不正确。关键字 self 引用“当前小部件实例”即 Alert/Popup 并且它没有属性 test.
File ".../main.py", line 33, in __init__
self.test.background_color = [0, 0, 1, 0.5]
AttributeError: 'Alert' object has no attribute 'test'
解决方案
定义按钮时,它被分配给一个对象btn1。因此,如果你想改变按钮的背景颜色,使用
btn1.background_color or sef.ids.test.background_colour or self.ids['test'].background_color
例子
main.py
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
Window.size = (500, 150)
class Alert(Popup):
def __init__(self, title, text):
super(Alert, self).__init__()
box = BoxLayout(orientation='vertical', padding=(5))
box.add_widget(Label(text=text))
btn1 = Button(text="Close")
self.ids['test'] = btn1
box.add_widget(btn1)
self.title = title
self.title_size = 30
self.title_align = 'center'
self.content = box
self.size_hint = (None, None)
self.size = (300, 200)
self.auto_dismiss = False
self.open()
self.ids.test.background_color = [0, 0, 1, 0.5]
class Test(App):
def build(self):
Alert(title='yeah!', text='inputs are invalid')
return
if __name__ == '__main__':
Test().run()