我正在使用python-2.7
和kivy-1.9.0
。有人能告诉我如何设置按钮的id
吗?
我正在尝试使用此代码设置按钮的test
。
btn1 = Button(text="Close",id="test")
但它提供了错误'Alert' object has no attribute 'test'
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()
答案 0 :(得分:0)
如果您在以后的情况下寻找使用按钮的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()
答案 1 :(得分:0)
您可以使用正确的方法在Python脚本中将 id 分配给按钮。
btn1 = Button(text="Close",id="test")
Python脚本中声明的 ID 与 kv文件中定义的 ID 不同。< / em>的
您遇到的错误不是因为未正确设置按钮的ID。关键字 self 引用“当前窗口小部件实例”,即警报/弹出窗口,它不是属性 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_colour
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()
btn1.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()