使用kv文件中的add_widget

huangapple go评论69阅读模式
英文:

Using add_widget in the kv file

问题

  1. 我在我的kv文件中动态添加小部件遇到了问题,到目前为止,我看到的都是关于在py文件中添加小部件的内容。我想要在kv文件中使用的原因是,我有一个修改过的按钮小部件SmoothButton,我想要在kv文件中使用它,但我不知道如何在py文件中创建它。

  2. 所以我猜有两个可能的问题:

  3. 如何在kv文件中正确使用add_widget。在下面的代码中,我得到了NameError: SmoothButton未定义的错误。

或者

  1. 如何在py文件中创建我的SmoothButton?

kv文件:

WindowManager:
    ChatPage:

<ChatPage>:
    name: "chat_page"

    NavigationLayout:
        id: nav_layout
        MDNavigationDrawer:
            NavigationDrawerIconButton:
                text: "Test"
                on_release: app.root.current = "login"

        FloatLayout:
            MDToolbar:
                pos_hint: {'top': 1}
                md_bg_color: 0.2, 0.6, 1, 1
                left_action_items: [['menu', lambda x: root.ids.nav_layout.toggle_nav_drawer()]]

            Button:
                text: "Create button"
                pos_hint: {'x': 0.65, 'top': 0.15}
                size_hint: 0.35, 0.15
                on_release: root.add_widget(SmoothButton)


<SmoothButton@Button>:
    background_color: 0,0,0,0
    background_normal: ""
    back_color: 1,0,1,1
    border_radius: [6]
    canvas.before:
        Color:
            rgba: self.back_color
        RoundedRectangle:
            size: self.size
            pos: self.pos
            radius: self.border_radius

py文件:

import kivy
from kivy.app import App
from kivy.properties import ObjectProperty, StringProperty, NumericProperty, ListProperty
from kivy.uix.screenmanager import ScreenManager, Screen
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 kivymd.theming import ThemeManager
import mysql.connector
from kivymd.uix.picker import MDDatePicker

class ChatPage(Screen):
    pass


class WindowManager(ScreenManager):
    pass


class MyApp(App):
    theme_cls = ThemeManager()

    def build(self):
        kv = Builder.load_file("kivy.kv")
        sm = WindowManager()

        screens = [ChatPage(name="chat_page")]
        for screen in screens:
            sm.add_widget(screen)

        sm.current = "chat_page"
        return sm


if __name__ == '__main__':
    MyApp().run()

希望这可以帮助你解决问题。

英文:

I'm having trouble dynamically adding widgets in my kv file, everything I've seen so far has been around adding widgets in the py file. The reason I'd like to use the kv file for this is that I've got a modified Button widget I'd like to use that's written in the kv file and I don't know how to create it in the py file.

So I guess there are two possible questions:

  1. How do I use add_widget correctly in the kv file. In the below code I get the NameError: SmoothButton is not defined.

Or

  1. How do I create my SmoothButton in the py file?

kv file:

WindowManager:
    ChatPage:

&lt;ChatPage&gt;:
    name: &quot;chat_page&quot;

    NavigationLayout:
        id: nav_layout
        MDNavigationDrawer:
            NavigationDrawerIconButton:
                text: &quot;Test&quot;
                on_release: app.root.current = &quot;login&quot;

        FloatLayout:
            MDToolbar:
                pos_hint: {&#39;top&#39;: 1}
                md_bg_color: 0.2, 0.6, 1, 1
                left_action_items: [[&#39;menu&#39;, lambda x: root.ids.nav_layout.toggle_nav_drawer()]]

            Button:
                text: &quot;Create button&quot;
                pos_hint: {&quot;x&quot;: 0.65, &quot;top&quot;: 0.15}
                size_hint: 0.35, 0.15
                on_release: root.add_widget(SmoothButton)


&lt;SmoothButton@Button&gt;:
    background_color: 0,0,0,0
    background_normal: &quot;&quot;
    back_color: 1,0,1,1
    border_radius: [6]
    canvas.before:
        Color:
            rgba: self.back_color
        RoundedRectangle:
            size: self.size
            pos: self.pos
            radius: self.border_radius

py file:

import kivy
from kivy.app import App
from kivy.properties import ObjectProperty, StringProperty, NumericProperty, ListProperty
from kivy.uix.screenmanager import ScreenManager, Screen
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 kivymd.theming import ThemeManager
import mysql.connector
from kivymd.uix.picker import MDDatePicker

class ChatPage(Screen):
    pass


class WindowManager(ScreenManager):
    pass


class MyApp(App):
    theme_cls = ThemeManager()

    def build(self):
        kv = Builder.load_file(&quot;kivy.kv&quot;)
        sm = WindowManager()

        screens = [ChatPage(name=&quot;chat_page&quot;)]
        for screen in screens:
            sm.add_widget(screen)

        sm.current = &quot;chat_page&quot;
        return sm


if __name__ == &#39;__main__&#39;:
    MyApp().run()
Thanks

</details>


# 答案1
**得分**: 2

如果你想在.kv文件中添加修改后的小部件,这并不意味着你必须在那里添加它。解决方案是将修改后的小部件在.kv文件中暴露给.py文件,并在"ChatPage"中创建一个方法,用于添加小部件:

***.py文件***

```python
# ...
class SmoothButton(Button):
    pass

class ChatPage(Screen):
    def on_released(self):
        self.add_widget(SmoothButton())

# ...

.kv文件

# ...
<ChatPage>:
    name: "chat_page"

    NavigationLayout:
        id: nav_layout
        MDNavigationDrawer:
            NavigationDrawerIconButton:
                text: "Test"
                on_release: app.root.current = "login"

        FloatLayout:
            MDToolbar:
                pos_hint: {'top': 1}
                md_bg_color: 0.2, 0.6, 1, 1
                left_action_items: [['menu', lambda x: root.ids.nav_layout.toggle_nav_drawer()]]

            Button:
                text: "Create button"
                pos_hint: {'x': 0.65, 'top': 0.15}
                size_hint: 0.35, 0.15
                on_release: root.on_released()  # <---

<SmoothButton>:  # <---
    background_color: 0, 0, 0, 0
    # ...

请注意,我已经将代码部分从你的原始内容中提取出来,只保留了翻译后的部分。

英文:

If you want to add modified widgets in the .kv it doesn't mean you have to add it there. The solution is to expose the modified widget in the .kv to the .py, and create a method in "ChatPage" where you add the widget:

*.py

<pre><code># ...
<b>class SmoothButton(Button):
pass</b>

class ChatPage(Screen):
def on_released(self):
self.add_widget(<b>SmoothButton()</b>)

...</code></pre>

*.kv

<pre><code># ...
&lt;ChatPage>:
name: "chat_page"

NavigationLayout:
    id: nav_layout
    MDNavigationDrawer:
        NavigationDrawerIconButton:
            text: &quot;Test&quot;
            on_release: app.root.current = &quot;login&quot;

    FloatLayout:
        MDToolbar:
            pos_hint: {&#39;top&#39;: 1}
            md_bg_color: 0.2, 0.6, 1, 1
            left_action_items: [[&#39;menu&#39;, lambda x: root.ids.nav_layout.toggle_nav_drawer()]]

        Button:
            text: &quot;Create button&quot;
            pos_hint: {&quot;x&quot;: 0.65, &quot;top&quot;: 0.15}
            size_hint: 0.35, 0.15
            on_release: &lt;b&gt;root.on_released()&lt;/b&gt; # &amp;lt;---

<b>&lt;SmoothButton>:</b> # &lt;---
background_color: 0,0,0,0
# ... </code></pre>

huangapple
  • 本文由 发表于 2020年1月3日 18:05:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定