将复选框添加到 Canva Kivy
Add CheckBox to a Canva Kivy
我需要给这个 Kivy 添加一个复选框 Canvas 但它不知道如何正确地添加它。我需要这个复选框来显示文件夹 Data 中所有文件的名称。然后用户检查他想要的文件,然后按下一个按钮(这里是红色的)它 return 文件名列表并调用一些函数。这就是我现在所在的位置。
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()
好的,您要的东西很多。
首先我们需要创建一个布局,将标签作为文件名,并选中相应文件的复选框。
为此,我将使用 GridLayout,但您可以根据需要进行操作。在此布局中,我们需要获取文件列表,创建带有复选框的标签,并最终查看标记了哪些框。所以我们将创建一个具有三个函数的 class 并使用字典将每个框 link 到其文件:
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传递给我们的主要内容以显示给用户。为此,我将使用弹出窗口,但同样,您可以随心所欲。所以我将开始将我们的内容添加到弹出窗口,并创建一个函数来调用它。
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()
我需要给这个 Kivy 添加一个复选框 Canvas 但它不知道如何正确地添加它。我需要这个复选框来显示文件夹 Data 中所有文件的名称。然后用户检查他想要的文件,然后按下一个按钮(这里是红色的)它 return 文件名列表并调用一些函数。这就是我现在所在的位置。
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()
好的,您要的东西很多。
首先我们需要创建一个布局,将标签作为文件名,并选中相应文件的复选框。
为此,我将使用 GridLayout,但您可以根据需要进行操作。在此布局中,我们需要获取文件列表,创建带有复选框的标签,并最终查看标记了哪些框。所以我们将创建一个具有三个函数的 class 并使用字典将每个框 link 到其文件:
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传递给我们的主要内容以显示给用户。为此,我将使用弹出窗口,但同样,您可以随心所欲。所以我将开始将我们的内容添加到弹出窗口,并创建一个函数来调用它。
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()