Python 3.x 在运行时将自定义小部件添加到屏幕
我想将按钮(基本上是带有图像的自定义按钮)作为自定义小部件添加到“Screen1”中,但最终总是出现“\u event.pyx not found”错误 我试过使用“super()。init(**kwargs)”和不使用 Python代码:Python 3.x 在运行时将自定义小部件添加到屏幕,python-3.x,kivy,Python 3.x,Kivy,我想将按钮(基本上是带有图像的自定义按钮)作为自定义小部件添加到“Screen1”中,但最终总是出现“\u event.pyx not found”错误 我试过使用“super()。init(**kwargs)”和不使用 Python代码: sm = ScreenManager() class DrinkWidget(Widget): pass class HomeScreen(BoxLayout): def switch(self, to): #Swith
sm = ScreenManager()
class DrinkWidget(Widget):
pass
class HomeScreen(BoxLayout):
def switch(self, to):
#Swithing funktion
#This is the Part, that causes the Problem I think:
class Screen1(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.add_widget(DrinkWidget(
lable_text_optn = 'test'
))
class Screen2(Screen):
pass
class ZapfanlageApp(App):
icon = 'GUI_Elemente/app_icon.png'
title = 'Zapfanlage'
def build(self):
pass
if __name__ == "__main__":
ZapfanlageApp().run()
Kivy代码(单独的.kv文件,“主屏幕”部分至今仍有效):
主屏幕:
sm:sm
名称:“屏幕管理器”
盒子布局:
方向:“垂直”
行数:2
操作栏:
位置提示:{'top':1}
大小提示y:.065
ActionView:
操作按钮:
文字:“鸡尾酒”
新闻界:
根开关(1)
操作按钮:
文字:“饮料”
新闻界:
根开关(2)
操作按钮:
文本:“Einstellungen”
新闻界:
根开关(3)
屏幕管理器:
id:sm
尺寸提示y:.935
屏幕1:
名称:“屏幕1”
id:屏幕1
屏幕2:
名称:“屏幕2”
id:屏幕2
:
名称:“屏幕1”
id:屏幕1
#此处应显示GridLayout中的按钮
:
名称:“屏幕2”
id:屏幕2
#这是我想在上面插入的自定义按钮
:
图像路径选项:图像路径
标签文本选项:标签文本
按钮:
大小提示:无
尺寸提示:无
高度:(根部高度)-10
宽度:250
新闻界:
盒子布局:
方向:“垂直”
宽度:root.width
高度:root.height
pos\u提示:root.pos
pos:root.pos
填充:5
图片:
来源:图像路径
标签:
文本:标签文本
我想在screen1
上垂直显示不同数量的DrinkWidget
s,并在运行时添加它们。但我总是以什么也不显示或出现\u event.pyx not found error
而告终。在:
下直接传递代码有效
我希望有人能帮助我。非常感谢 好的,当你的应用程序加载时,看起来你想在屏幕上添加一些你的
DrinkWidget
s。首先,在.py
文件中,您定义了一个名为Drink\u widget
的类,但在.kv
中,您称之为DrinkWidget
接下来,由于您的DrinkWidget
定义为从kivy继承按钮
类,因此可以使用text:
字段轻松更改DrinkWidget
中的文本。类似地,您可以使用background\u normal:
字段将按钮显示的图像更改为您喜欢的任何图像。要更改单击按钮时显示的图像,请使用background\u down:
字段。例如:
<DrinkWidget@Button>:
text: "some text"
background_normal: "image1.png"
background_down: "image2.png"
main.kv
GridLayout:
cols: 1
Screen:
# Widgets that aren't Layouts normally only have 1 child widget
# To "add multiple widgets" to a screen, give the screen a Layout, then add widgets to the layout
GridLayout:
id: some_descriptive_id
rows: 1
您的代码有点太长,无法为您的案例提供精确的解决方案,但我希望这个示例能够为您提供知识,让您自己修复它 你能编辑你的文章,让所有代码都有适当的缩进吗?此外,当您尝试编写“Kivy代码”时:“看起来您本打算将该代码放在代码块中。@Erik感谢您的提示。我希望现在能更好地解释它,并标记出最相关的点。经过一点测试,我让它工作了!非常感谢你!你的回答很好,可以理解!太棒了,这就是我喜欢听的!
from kivy.app import App
from kivy.uix.button import Button
class DrinkWidget(Button):
pass
class MainApp(App):
def on_start(self):
# This command is automatically called when your app loads up
the_screen_grid = self.root.ids.some_descriptive_id
# self.root refers to the root widget, which is the GridLayout
# self.root.ids gets me a DictProperty of all children widgets that have an id associated with them
# self.root.ids.some_descriptive_id gets me the GridLayout widget I defined with the id: some_descriptive_id
for i in range(3):
the_screen_grid.add_widget(DrinkWidget(text="drink " + str(i)))
MainApp().run()
GridLayout:
cols: 1
Screen:
# Widgets that aren't Layouts normally only have 1 child widget
# To "add multiple widgets" to a screen, give the screen a Layout, then add widgets to the layout
GridLayout:
id: some_descriptive_id
rows: 1