【问题标题】:Add CheckBox to a Canva Kivy将复选框添加到 Canva Kivy
【发布时间】:2020-12-06 09:57:15
【问题描述】:

我需要在此 Kivy Canvas 中添加一个复选框,但它不知道如何正确执行此操作。我需要该复选框来显示文件夹 Data 中所有文件的名称。然后用户检查他想要的文件然后按下一个按钮(这里是红色的),它会返回一个文件名列表并调用一些函数。这就是我现在的位置。

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.metrics import dp
from kivy.lang import Builder

from kivy.uix.label import Label

from kivy.uix.button import Button
from kivy.base import runTouchApp

from kivy.uix.recycleview import RecycleView

from kivy.uix.popup import Popup



from kivy.uix.progressbar import ProgressBar 
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.widget import Widget.
from kivy.properties import ObjectProperty




 import time
 import os

Builder.load_string('''<Game>
    ShootButton:


<ShootButton>

    Image: 
        source: 'logo.jpg'
        allow_stretch: True
        size: 300,300
        pos: 300, 400


    Image: 
        source: 'livre.jpg'
        allow_stretch: True
        size: 350, 350
        pos: 490,130

    Button:
        text: "Modifier Correspondance"
        size: 240,50
        font_size: 20
        pos: 100,300
        background_color :0, 1, 0, 1,
        on_press: root.shoot()

    Button:
        text: "Liste Fichiers"
        size: 240,50
        font_size: 20
        pos: 100,400
        background_color :0, 1, 0, 1,
        on_press: root.fire_popup()

    Button:
        text: "Ajouter Fichier"
        size: 240,50
        font_size: 20
        pos: 100,200
        background_color :0, 1, 0, 1,
        on_press: root.shoot2()

    Button:
        text: "Generer PDF"
        size: 180,50
        font_size: 20
        pos: 580,60
        background_color :1, 0, 0, 1,
        on_press: root.display()



         ''')


 Window.size = (900, 600)
 Window.clearcolor = (1, 1, 1, 1)




class Game(Widget):
    pass


class SimplePopup(Popup):
    pass

class SimplePopup2(Popup):
    pass

class ShootButton(Widget):

    def shoot(self):
        shooting = Bullet()
        shooting.bullet_fly()

    def shoot2(self):
        shooting = Bullet()
        shooting.bullet_fly2()

class Bullet(Widget):
    def bullet_fly(self):
        print("test")


    def bullet_fly2(self):
        os.system("open .")





class MyApp(App):
    def build(self):
        return Game()

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

【问题讨论】:

    标签: python checkbox kivy


    【解决方案1】:

    好的,你要的东西很多。

    首先,我们需要创建一个布局,其中标签作为文件名,复选框对应文件。

    为此,我将使用 GridLayout,但您可以随心所欲。在这个布局中,我们需要获取文件列表,使用复选框创建标签,最后查看标记了哪些框。因此,我们将创建一个具有三个函数的类,并使用一个 dict 将每个框链接到它的文件:

    class Grid(GridLayout):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
            self.cols = 2
    
            self.files = self.get_files()
            self.check_list = {}
            self.create_checks()
    
    
        def get_files(self):
            # Get your files herre
            return ['Hi', 'how', 'are', 'you?']
    
        def create_checks(self):
            # Create the checkboxes
            for item in self.files:
                self.add_widget(Label(text=item))
                self.check_list[item] = CheckBox()
                self.add_widget(self.check_list[item])
    
        def get_actives(self):
            # Check who is active (marked)
            actives = []
            for item in self.check_list:
                if self.check_list[item].state == 'down':
                    actives.append(item)
    
            return actives
    

    然后我们需要将这个类传递给我们的主要内容以向用户显示它。为此,我将使用弹出窗口,但同样,您可以随心所欲。所以我将开始将我们的内容添加到弹出窗口中,并创建一个函数来调用它。

    class ShootButton(Widget):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
    
            # Create popup with grid content
            self.pop_content = Grid()
            self.popup = Popup(title='', separator_height=0,
                               size_hint=(.8, .6),
                               content=self.pop_content)
    
        def open_pop(self):
            # You guessed! Opens the popup
            self.popup.open()
    

    大部分完成,现在我们只需要检查按下按钮时标记了哪些复选框。所以我会得到你的函数display并用它来调用我们的get_actives函数:

    def display(self):
        actives = self.pop_content.get_actives()
        print(actives)
    

    这是完整的代码:

    from kivy.app import App
    from kivy.core.window import Window
    from kivy.lang import Builder
    from kivy.uix.popup import Popup
    from kivy.uix.widget import Widget
    from kivy.uix.gridlayout import GridLayout
    from kivy.uix.checkbox import CheckBox
    from kivy.uix.label import Label
    import os
    
    Builder.load_string('''<Game>
        ShootButton:
    
    
    <ShootButton>
    
        Image: 
            source: 'logo.jpg'
            allow_stretch: True
            size: 300,300
            pos: 300, 400
    
    
        Image: 
            source: 'livre.jpg'
            allow_stretch: True
            size: 350, 350
            pos: 490,130
    
        Button:
            text: "Modifier Correspondance"
            size: 240,50
            font_size: 20
            pos: 100,300
            background_color :0, 1, 0, 1,
            on_press: root.shoot()
    
        Button:
            text: "Liste Fichiers"
            size: 240,50
            font_size: 20
            pos: 100,400
            background_color :0, 1, 0, 1,
            on_press: root.fire_popup()
    
        Button:
            text: "Ajouter Fichier"
            size: 240,50
            font_size: 20
            pos: 100,200
            background_color :0, 1, 0, 1,
            on_press: root.shoot2()
    
        Button:
            text: "Generer PDF"
            size: 180,50
            font_size: 20
            pos: 580,60
            background_color :1, 0, 0, 1,
            on_press: root.display()
             ''')
    
    Window.size = (900, 600)
    Window.clearcolor = (1, 1, 1, 1)
    
    
    class Game(Widget):
        pass
    
    
    class SimplePopup(Popup):
        pass
    
    
    class SimplePopup2(Popup):
        pass
    
    
    class Grid(GridLayout):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
            self.cols = 2
    
            self.files = self.get_files()
            self.check_list = {}
            self.create_checks()
    
        def get_files(self):
            # Get your files here
            return ['Hi', 'how', 'are', 'you?']
    
        def create_checks(self):
            # Create the checkboxes
            for item in self.files:
                self.add_widget(Label(text=item))
                self.check_list[item] = CheckBox()
                self.add_widget(self.check_list[item])
    
        def get_actives(self):
            # Check who is active (marked)
            actives = []
            for item in self.check_list:
                if self.check_list[item].state == 'down':
                    actives.append(item)
    
            return actives
    
    
    class ShootButton(Widget):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
    
            # Create popup with grid content
            self.pop_content = Grid()
            self.popup = Popup(title='Choose files', separator_height=0,
                               size_hint=(.8, .6),
                               content=self.pop_content)
    
        def shoot(self):
            shooting = Bullet()
            shooting.bullet_fly()
            self.open_pop()
    
        def shoot2(self):
            shooting = Bullet()
            shooting.bullet_fly2()
    
        def fire_popup(self):
            pass
    
        def open_pop(self):
            # You guessed! Opens the popup
            self.popup.open()
    
        def display(self):
            actives = self.pop_content.get_actives()
            print(actives)
    
    
    class Bullet(Widget):
        def bullet_fly(self):
            print("test")
    
        def bullet_fly2(self):
            os.system("open .")
    
    
    class MyApp(App):
        def build(self):
            return Game()
    
    
    if __name__ == '__main__':
        MyApp().run()
    

    【讨论】:

    • 奇怪,您是否只是复制了此答案中的完整代码?你在用python 2吗?
    • 感谢您的回答。我正在使用 Python 2.7,所以我修改了 super.我没有看到复选框,但是当我单击按钮时会弹出一个空白列表
    猜你喜欢
    • 2017-02-07
    • 2015-11-22
    • 2015-08-13
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多