当前位置: 首页 > 知识库问答 >
问题:

刷新kivy小部件的问题

武琛
2023-03-14

我在刷新小部件的BoxLayout时遇到了问题,方法是删除它们,然后根据列表“组”重新构建小部件。在EditDeviceGroups屏幕上,“创建”按钮应向列表中添加一个元素,并将用户转发到GroupTemplateScreen,确实如此。

当用户使用“后退”按钮返回到EditDeviceGroups屏幕时,会出现此问题。当时,我认为on_enter方法将刷新小部件以包含新元素,但列表中没有显示任何更改。

我认为这是类和实例的某种问题,但我不能完全理解这一点,因为这是我第一次真正尝试使用Kivy。

soundclout.py

from kivy.app import App
from kivy.properties import ObjectProperty, ListProperty, StringProperty
from kivy.uix.listview import ListItemButton
from kivy.uix.screenmanager import ScreenManager, Screen, WipeTransition
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import Button,Label
from kivy.graphics import Color,Rectangle,InstructionGroup

class GroupTemplateScreen(Screen):
    
    def remove(self):
        pass

class HomeScreen(Screen):
    skipBuild = 'build_timeline_screen_6'

    #skips build option if already timeline is already built
    def skip_build_screen(self,value):
        if value is 1:
            print('HomeScreen.skip_build_screen')
            self.skipBuild = 'edit_timeline_screen_7'

class EditDeviceGroupsScreen(Screen):
    #Groups = [GroupNo,null]
    Groups = [[1,10],[2,20]]

    def on_enter(self):
        self.ids.glayout2.clear_widgets()
        for i in xrange(0,len(EditDeviceGroupsScreen().Groups)):
            ##THISPRINT##
            print(str(EditDeviceGroupsScreen().Groups[i][0]))
            addedGroup = BoxLayout(size_hint_y=None,height='120sp',orientation='horizontal')
            addedButton=Button(text="Group " + str(EditDeviceGroupsScreen().Groups[i][0]) + " Settings",font_size=25)
            addedGroup.add_widget(addedButton)
            self.ids.glayout2.add_widget(addedGroup)

    #Removes all widget on leaving to prevent the creation of duplicate widgets
    def nav_to_group(self):
        self.manager.current = 'edit_group_behaviour_screen_9'

    def create_group(self):
        base = 1
        for i in xrange(0,len(EditDeviceGroupsScreen().Groups)):
            if base < EditDeviceGroupsScreen().Groups[i][0]:
                base = EditDeviceGroupsScreen().Groups[i][0]
        EditDeviceGroupsScreen().Groups.append([base+1,(base+1)*10])        

#manages screens
class Manager(ScreenManager):

    home_screen = ObjectProperty()
    edit_device_groups_screen = ObjectProperty()
    group_template_screen= ObjectProperty()

    def update(self):
        self.connected_device_list._trigger_reset_populate()
        self.current_screen.update()

class SoundCloutApp(App):
    
    def build(self):
        return Manager(transition=WipeTransition())

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

soundclout.kv

#: kivy 1.10.0
#: import hex kivy.utils.get_color_from_hex
#: import main soundclout
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import ListItemButton kivy.uix.listview.ListItemButton
#: import ScrollView kivy.uix.scrollview


<GroupTemplateScreen>:
    rows: 2
    spacing: 10
    canvas:
        Color:
            rgba: 1,1,1,1
        Rectangle:
            pos: self.pos
            size: self.size

    BoxLayout:
        size_hint_y: None
        height: 50
        spacing: 10
        pos: root.x, (root.height-50)
 
        # Make the background for the toolbar blue
        canvas:
            Color:
                rgba: hex('#0099cc')
            Rectangle:
                pos: self.pos
                size: self.size
        Button:
            on_press: root.manager.current = 'homescreen_1'
            background_normal: 'icons/home.png'
            size_hint_x: None
            width:50

    BoxLayout:
        size_hint_y: None
        height: 500
        spacing: 50
        padding: 20
        pos: root.x, (root.height-560)

        BoxLayout:
            orientation: "vertical"
            spacing: 10
            padding: 10
            canvas:
                Color:
                    rgba: hex('#0099cc')
                Rectangle:
                    pos: self.pos
                    size: self.size
           
            BoxLayout:
                size_hint_y: None
                orientation: "horizontal"
                height: 50
                spacing: 10
                padding: 0
                canvas:
                    Color:
                        rgba: hex('#0099cc')
                    Rectangle:
                        pos: self.pos
                        size: self.size   
                Button:
                    size_hint_x: None
                    size_hint_y: None
                    height: 50
                    width:100
                    text: 'Back'
                    on_release: root.manager.current = 'edit_device_groups_screen_5'
       
                Button:
                    size_hint_x: None
                    size_hint_y: None
                    height: 50
                    width:100
                    text: 'Remove'

                    #have to change first argument, for now assume switch is on
                    on_press: root.remove()
                    on_release:root.manager.current = 'edit_device_groups_screen_5'

            BoxLayout:
                orientation: "vertical"
                spacing: 10
                padding: 10
                canvas:
                    Color:
                        rgba: hex('#ffffff')
                    Rectangle:
                        pos: self.pos
                        size: self.size

                #Devices connected
                BoxLayout:
                    orientation: "horizontal"
                    canvas:
                        Color:
                            rgba: hex('#98a6b3')
                        Rectangle:
                            pos: self.pos
                            size: self.size

                    Label:
                        text: 'Name:'
                        font_size: 24
                        
                        #TODO
                    Label:
                        text: 'Group 1'
                        font_size: 24

                #Devices connected
                BoxLayout:
                    orientation: "horizontal"
                    canvas:
                        Color:
                            rgba: hex('#98a6b3')
                        Rectangle:
                            pos: self.pos
                            size: self.size

                    Label:
                        text: 'Devices:'
                        font_size: 24
                        
                    #TODO
                    Label:
                        text: '1,2,3,4'
                        font_size: 24

<HomeScreen>:
    rows: 2
    spacing: 10
    canvas:
        Color:
            rgba: 1,1,1,1
        Rectangle:
            pos: self.pos
            size: self.size

    BoxLayout:
        size_hint_y: None
        height: 50
        spacing: 10
        pos: root.x, (root.height-50)
 
        # Make the background for the toolbar blue
        canvas:
            Color:
                rgba: hex('#0099cc')
            Rectangle:
                pos: self.pos
                size: self.size
        Button:
            on_press: root.manager.current = 'homescreen_1'
            background_normal: 'icons/home.png'
            size_hint_x: None
            width:50

    BoxLayout:
        size_hint_y: None
        height: 500
        spacing: 50
        padding: 50
        pos: root.x, (root.height-560)

        BoxLayout:
            orientation: "vertical"
            spacing: 10
            padding: 10
            canvas:
                Color:
                    rgba: hex('#0099cc')
                Rectangle:
                    pos: self.pos
                    size: self.size

            Button:
                text: "Start"
                font_size: 20

            Button:
                text: "Device Tester"
                font_size: 20

            Button:
                text: "Connect Devices"
                font_size: 20

            Button:
                text: "Edit Device Groups"
                font_size: 20
                on_press: root.manager.current = 'edit_device_groups_screen_5'

            Button:
                text: "Edit Group Behavior"
                font_size: 20

        BoxLayout:
            orientation: "vertical"
            spacing: 10
            padding: 10
            canvas:
                Color:
                    rgba: hex('#0099cc')
                Rectangle:
                    pos: self.pos
                    size: self.size
            Label:
                text: "Connected Devices"
                font_size: 30

<EditDeviceGroupsScreen>:
    rows: 2
    spacing: 10
    canvas:
        Color:
            rgba: 1,1,1,1
        Rectangle:
            pos: self.pos
            size: self.size

    #ToolBar
    BoxLayout:
        size_hint_y: None
        height: 50
        spacing: 10
        pos: root.x, (root.height-50)
 
        # Make the background for the toolbar blue
        canvas:
            Color:
                rgba: hex('#0099cc')
            Rectangle:
                pos: self.pos
                size: self.size
        Button:
            on_press: root.manager.current = 'homescreen_1'
            background_normal: 'icons/home.png'
            size_hint_x: None
            width:50

    BoxLayout:
        size_hint_y: None
        height: 500
        spacing: 50
        padding: 20
        pos: root.x, (root.height-560)

        BoxLayout:
            orientation: "vertical"
            spacing: 10
            padding: 10
            canvas:
                Color:
                    rgba: hex('#0099cc')
                Rectangle:
                    pos: self.pos
                    size: self.size
            Button:
                size_hint_x: None
                size_hint_y: None
                height: 50
                size: self.size
                text: 'create group'
                on_press: main.EditDeviceGroupsScreen().create_group()
                on_release: root.manager.current = 'group_template_screen_11'

            #Groups holder
            BoxLayout:
                orientation: "vertical"
                spacing: 5
                padding: 5
                canvas:
                    Color:
                        rgba: hex('#ffffff')
                    Rectangle:
                        pos: self.pos
                        size: self.size

                #Adds Scrollability
                ScrollView:
                    do_scroll_x:False
                    do_scroll_Y:True
                    BoxLayout:
                        id: glayout2
                        orientation: 'vertical'
                        spacing: 5
                        size_hint_y: None
                        height: self.minimum_height


<Manager>:
    id: screen_manager
    home_screen: home_screen
    edit_device_groups_screen: edit_device_groups_screen
    group_template_screen: group_template_screen

    HomeScreen:
        id: home_screen
        name: 'homescreen_1'
        manager: screen_manager

    EditDeviceGroupsScreen:
        id: edit_device_groups_screen
        name: 'edit_device_groups_screen_5'
        manager: screen_manager

    GroupTemplateScreen:
        id: group_template_screen
        name: 'group_template_screen_11'
        manager: screen_manager

共有1个答案

西门品
2023-03-14

#问题#EditDeviceGroup屏幕没有用新添加的小部件刷新,因为当按下后退按钮时,EditDeviceGroupScreen的另一个实例被实例化。

#解决方案#通过以下更改,当用户单击“上一步”按钮返回EditDeviceGroups屏幕时,on_enter方法刷新小部件以包含添加的新html" target="_blank">元素。有关详细信息,请参考示例和输出。

##soundclout.kv##

  1. 删除#:导入主声音
  2. 替换了main。create_grouproot.create_group

##soundclout.py##

  1. 替换EditDeviceGroupsScreen()的所有匹配项。和自己

#示例####main.py##

from kivy.app import App
from kivy.properties import ObjectProperty, ListProperty, StringProperty
from kivy.uix.listview import ListItemButton
from kivy.uix.screenmanager import ScreenManager, Screen, WipeTransition
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import Button, Label
from kivy.graphics import Color, Rectangle, InstructionGroup


class GroupTemplateScreen(Screen):

    def remove(self):
        pass


class HomeScreen(Screen):
    skipBuild = 'build_timeline_screen_6'

    # skips build option if already timeline is already built
    def skip_build_screen(self, value):
        if value is 1:
            print('HomeScreen.skip_build_screen')
            self.skipBuild = 'edit_timeline_screen_7'


class EditDeviceGroupsScreen(Screen):
    # Groups = [GroupNo, null]
    Groups = [[1, 10], [2, 20]]

    def on_enter(self):
        print("EditDeviceGroupsScreen.on_enter:")
        self.ids.glayout2.clear_widgets()
        for i in range(0, len(self.Groups)):
            ##THISPRINT##
            print(str(self.Groups[i][0]))
            addedGroup = BoxLayout(size_hint_y=None, height='120sp', orientation='horizontal')
            addedButton = Button(text="Group " + str(self.Groups[i][0]) + " Settings", font_size=25)
            addedGroup.add_widget(addedButton)
            self.ids.glayout2.add_widget(addedGroup)

    # Removes all widget on leaving to prevent the creation of duplicate widgets
    def nav_to_group(self):
        self.manager.current = 'edit_group_behaviour_screen_9'

    def create_group(self):
        base = 1
        for i in range(0, len(self.Groups)):
            if base < self.Groups[i][0]:
                base = self.Groups[i][0]
        self.Groups.append([base+1, (base+1)*10])


# manages screens
class Manager(ScreenManager):

    home_screen = ObjectProperty()
    edit_device_groups_screen = ObjectProperty()
    group_template_screen= ObjectProperty()

    def update(self):
        self.connected_device_list._trigger_reset_populate()
        self.current_screen.update()


class SoundCloutApp(App):

    def build(self):
        return Manager(transition=WipeTransition())


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

##soundclout.kv##

#:kivy 1.10.0
#:import hex kivy.utils.get_color_from_hex
#:import ListAdapter kivy.adapters.listadapter.ListAdapter
#:import ListItemButton kivy.uix.listview.ListItemButton
#:import ScrollView kivy.uix.scrollview


<GroupTemplateScreen>:
    rows: 2
    spacing: 10
    canvas:
        Color:
            rgba: 1,1,1,1
        Rectangle:
            pos: self.pos
            size: self.size

    BoxLayout:
        size_hint_y: None
        height: 50
        spacing: 10
        pos: root.x, (root.height-50)

        # Make the background for the toolbar blue
        canvas:
            Color:
                rgba: hex('#0099cc')
            Rectangle:
                pos: self.pos
                size: self.size
        Button:
            on_press: root.manager.current = 'homescreen_1'
            background_normal: 'icons/home.png'
            size_hint_x: None
            width:50

    BoxLayout:
        size_hint_y: None
        height: 500
        spacing: 50
        padding: 20
        pos: root.x, (root.height-560)

        BoxLayout:
            orientation: "vertical"
            spacing: 10
            padding: 10
            canvas:
                Color:
                    rgba: hex('#0099cc')
                Rectangle:
                    pos: self.pos
                    size: self.size

            BoxLayout:
                size_hint_y: None
                orientation: "horizontal"
                height: 50
                spacing: 10
                padding: 0
                canvas:
                    Color:
                        rgba: hex('#0099cc')
                    Rectangle:
                        pos: self.pos
                        size: self.size
                Button:
                    size_hint_x: None
                    size_hint_y: None
                    height: 50
                    width:100
                    text: 'Back'
                    on_release: root.manager.current = 'edit_device_groups_screen_5'

                Button:
                    size_hint_x: None
                    size_hint_y: None
                    height: 50
                    width:100
                    text: 'Remove'

                    #have to change first argument, for now assume switch is on
                    on_press: root.remove()
                    on_release:root.manager.current = 'edit_device_groups_screen_5'

            BoxLayout:
                orientation: "vertical"
                spacing: 10
                padding: 10
                canvas:
                    Color:
                        rgba: hex('#ffffff')
                    Rectangle:
                        pos: self.pos
                        size: self.size

                #Devices connected
                BoxLayout:
                    orientation: "horizontal"
                    canvas:
                        Color:
                            rgba: hex('#98a6b3')
                        Rectangle:
                            pos: self.pos
                            size: self.size

                    Label:
                        text: 'Name:'
                        font_size: 24

                        #TODO
                    Label:
                        text: 'Group 1'
                        font_size: 24

                #Devices connected
                BoxLayout:
                    orientation: "horizontal"
                    canvas:
                        Color:
                            rgba: hex('#98a6b3')
                        Rectangle:
                            pos: self.pos
                            size: self.size

                    Label:
                        text: 'Devices:'
                        font_size: 24

                    #TODO
                    Label:
                        text: '1,2,3,4'
                        font_size: 24

<HomeScreen>:
    rows: 2
    spacing: 10
    canvas:
        Color:
            rgba: 1,1,1,1
        Rectangle:
            pos: self.pos
            size: self.size

    BoxLayout:
        size_hint_y: None
        height: 50
        spacing: 10
        pos: root.x, (root.height-50)

        # Make the background for the toolbar blue
        canvas:
            Color:
                rgba: hex('#0099cc')
            Rectangle:
                pos: self.pos
                size: self.size
        Button:
            on_press: root.manager.current = 'homescreen_1'
            background_normal: 'icons/home.png'
            size_hint_x: None
            width:50

    BoxLayout:
        size_hint_y: None
        height: 500
        spacing: 50
        padding: 50
        pos: root.x, (root.height-560)

        BoxLayout:
            orientation: "vertical"
            spacing: 10
            padding: 10
            canvas:
                Color:
                    rgba: hex('#0099cc')
                Rectangle:
                    pos: self.pos
                    size: self.size

            Button:
                text: "Start"
                font_size: 20

            Button:
                text: "Device Tester"
                font_size: 20

            Button:
                text: "Connect Devices"
                font_size: 20

            Button:
                text: "Edit Device Groups"
                font_size: 20
                on_press: root.manager.current = 'edit_device_groups_screen_5'

            Button:
                text: "Edit Group Behavior"
                font_size: 20

        BoxLayout:
            orientation: "vertical"
            spacing: 10
            padding: 10
            canvas:
                Color:
                    rgba: hex('#0099cc')
                Rectangle:
                    pos: self.pos
                    size: self.size
            Label:
                text: "Connected Devices"
                font_size: 30

<EditDeviceGroupsScreen>:
    rows: 2
    spacing: 10
    canvas:
        Color:
            rgba: 1,1,1,1
        Rectangle:
            pos: self.pos
            size: self.size

    #ToolBar
    BoxLayout:
        size_hint_y: None
        height: 50
        spacing: 10
        pos: root.x, (root.height-50)

        # Make the background for the toolbar blue
        canvas:
            Color:
                rgba: hex('#0099cc')
            Rectangle:
                pos: self.pos
                size: self.size
        Button:
            on_press: root.manager.current = 'homescreen_1'
            background_normal: 'icons/home.png'
            size_hint_x: None
            width:50

    BoxLayout:
        size_hint_y: None
        height: 500
        spacing: 50
        padding: 20
        pos: root.x, (root.height-560)

        BoxLayout:
            orientation: "vertical"
            spacing: 10
            padding: 10
            canvas:
                Color:
                    rgba: hex('#0099cc')
                Rectangle:
                    pos: self.pos
                    size: self.size
            Button:
                size_hint_x: None
                size_hint_y: None
                height: 50
                size: self.size
                text: 'create group'
                on_press: root.create_group()
                on_release: root.manager.current = 'group_template_screen_11'

            #Groups holder
            BoxLayout:
                orientation: "vertical"
                spacing: 5
                padding: 5
                canvas:
                    Color:
                        rgba: hex('#ffffff')
                    Rectangle:
                        pos: self.pos
                        size: self.size

                #Adds Scrollability
                ScrollView:
                    do_scroll_x:False
                    do_scroll_Y:True
                    BoxLayout:
                        id: glayout2
                        orientation: 'vertical'
                        spacing: 5
                        size_hint_y: None
                        height: self.minimum_height


<Manager>:
    id: screen_manager
    home_screen: home_screen
    edit_device_groups_screen: edit_device_groups_screen
    group_template_screen: group_template_screen

    HomeScreen:
        id: home_screen
        name: 'homescreen_1'
        manager: screen_manager

    EditDeviceGroupsScreen:
        id: edit_device_groups_screen
        name: 'edit_device_groups_screen_5'
        manager: screen_manager

    GroupTemplateScreen:
        id: group_template_screen
        name: 'group_template_screen_11'
        manager: screen_manager
 类似资料:
  • 我试着用Kivy模仿flappy birds,但目前我的处境很艰难。问题是我不知道如何在视图中创建多个小部件。 现在我的kv文件中有: 目前我能够制作单列,所以问题是如何制作才能有多列?

  • 我正在使用Kivy python库。 我定义了两个小部件。 当程序运行时,我运行第一个小部件。 当按下widgets按钮时,我希望它消失并被第二个widget替换。 这是两个小部件的. kv 我的主python文件运行应用程序,并返回第一个小部件 我的第一个小部件有一个回调。这就是问题代码所属的位置 这里的想法是有一个用户界面管理器。此管理器不像树那样运行UI,而是像列表和堆栈一样运行UI。该列表

  • 我想问一下,我如何在我的应用程序中动态添加一些小部件,一个接一个,而不是一次添加完。这些小部件被添加到包含命令的for循环中,并由按钮触发。所以我想知道是否有办法在执行结束时逐渐显示输出,而不是一次显示完。起初,我试图在for循环中添加延迟,但恐怕这与每次构建输出的方式有关。 编辑:嗯,似乎我没有很好地理解和的用法,所以我对他们(或time.sleep)的尝试没有成功在所有。但显然,这是我问题的解

  • 我试图在屏幕中间构造一个窗口小部件。我没有使用pos_hint或size_hint,因为我将在以后更改小部件的位置,但是当我构建小部件时,它的大小和位置不正确。这是我的密码: 为什么小部件的大小不等于窗口大小的十分之一,为什么它的中心在窗口的右上角?

  • 我在kivy中添加或删除小部件时遇到一些困难。情况就是这样: 主窗体应该包含三个小部件中的两个,即Widget1、Widget2和Widget3。按下Widget1的按钮,Widget2应该被删除,Widget3应该出现。 这是main.py文件: 这是.kv文件: 在类表单中,我检查Widgets1和2是否为该类的子类: 我得到: 因此,当我尝试删除现有子项并添加新子项时,我得到: 有人能帮忙吗

  • 我一直在玩KivyPong教程,了解框架的最新情况,看看是否可以实现一些想法。我已经删除了大部分乒乓球功能,所以我只能在屏幕上显示弹跳球,并添加了一些代码来生成屏幕上的多个弹跳球,在触摸时生成。效果很好。然后,我添加了一些额外的画布说明,因此我将绘制一条线,指示球移动的方向。这就是事情变得奇怪的地方。第一个球的动作就像它应该做的一样,在屏幕上弹跳。但接下来的任何点击都会产生球,球会离开屏幕,随机改