我想将“按钮”(基本上是带有图像的自定义按钮)添加为“ Screen1”的自定义小部件,但是我总是以“ _event.pyx not found”错误结束。
我尝试使用“ super()。 init (** kwargs)”,而没有使用。
Python代码:
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文件。到目前为止,“ HomeScreen”部分仍然有效):
HomeScreen:
sm: sm
name: 'ScreenManager'
BoxLayout:
orientation: 'vertical'
rows: 2
ActionBar:
pos_hint: {'top': 1}
size_hint_y: .065
ActionView:
ActionButton:
text: 'Cocktails'
on_press:
root.switch(1)
ActionButton:
text: 'Drinks'
on_press:
root.switch(2)
ActionButton:
text: 'Einstellungen'
on_press:
root.switch(3)
ScreenManager:
id: sm
size_hint_y: .935
Screen1:
name: "screen1"
id: screen1
Screen2:
name: "screen2"
id: screen2
<Screen1@Screen>:
name: "screen_1"
id: screen1
#Here should the Buttons in GridLayout appear
<Screen2@Screen>:
name: "screen_2"
id: screen2
#This is the Custom Button I want to be inserted above
<Drink_Widget@Button>:
image_path_optn: image_path
lable_text_optn: lable_text
Button:
size_hint_x: None
size_hint_y: None
height: (root.height) -10
width: 250
on_press:
BoxLayout:
orientation: "vertical"
width: root.width
height: root.height
pos_hint: root.pos
pos: root.pos
padding: 5
Image:
source: image_path
Label:
text: label_text
我想在DrinkWidget
上垂直显示各种screen1
,并在运行时添加它们。但是,我总是最终什么也没有出现,或者以_event.pyx not found error
结尾。直接在<Screen1@Screen>:
下传递代码。
我希望有人能帮助我。非常感谢!
答案 0 :(得分:1)
好的,看来您想在应用加载时向屏幕上添加一些DrinkWidget
。首先,在.py
文件中定义了一个名为Drink_widget
的类,但在.kv
中将其称为DrinkWidget
接下来,由于已将DrinkWidget
定义为从kivy继承Button
类,因此可以使用DrinkWidget
字段轻松更改text:
中的文本。同样,您可以使用background_normal:
字段将按钮显示的图像更改为所需的图像。要更改单击按钮时显示的图像,请使用background_down:
字段。示例:
<DrinkWidget@Button>:
text: "some text"
background_normal: "image1.png"
background_down: "image2.png"
因此,您不需要lable_text_optn
或image_path_optn
字段。
此外,您实际上是尝试向Screen
小部件添加许多小部件,实际上,您实际上应该向Layout
小部件(FloatLayout
,{{ 1}}或BoxLayout
)。您的GridLayout
小部件应仅将Screen
小部件作为其直接子级。
我看到的另一个问题是,Layout
文件中有两个 root 小部件-.kv
和HomeScreen
,除非您的缩进在问题中是正确的。
以下是我认为您正在努力工作的一个最小示例:
BoxLayout
main.py
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()
main.kv
您的代码太长了,无法为您的情况提供确切的解决方案,但我希望此示例为您提供知识,以便您自己进行修复!