如何在 Kivy 中使用 Popup 运行复选框小部件?
Posted
技术标签:
【中文标题】如何在 Kivy 中使用 Popup 运行复选框小部件?【英文标题】:How to run checkbox widget using Popup in Kivy? 【发布时间】:2021-11-20 01:33:31 【问题描述】:这里是新手 kivy 用户。我已经创建了 2 个 Kivy 应用程序,每个应用程序都可以单独运行,但我无法将一个集成到另一个应用程序中。我正在尝试使用 Popup(onButtonPress3
) 在MyApp
中运行CheckBoxesApp
。当我运行我的代码时,我收到错误“WidgetException: add_widget() can be used only with instances of the Widget class.
”可能与 CheckBoxesApp 使用 App 而 MyApp 使用 MDApp 的事实有关?任何帮助表示赞赏>>
main.py
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.app import App
from kivy.uix.widget import Widget
import kivy
kivy.require('1.10.1')
from kivymd.app import MDApp
from kivy.uix.popup import Popup
FruitsSellected = []
class Checkboxes(Widget):
def checkboxes_click(self, instance, value, text):
if value:
print(f"Select text")
self.myvar = FruitsSellected
#Close
if text == 'FINISH':
print(FruitsSellected)
self.save()
App.get_running_app().stop()
return FruitsSellected
FruitsSellected.append(str(text))
else:
print(f"UnSelect text")
FruitsSellected.remove(str(text))
def save(self):
with open("allergenlist.txt", "w") as fobj:
fobj.write(str(self.myvar))
class CheckBoxesApp(App):
def build(self):
return Checkboxes()
class MyApp(MDApp):
def build(self):
# Define a grid layout for this App
self.layout = GridLayout(cols=1, padding=10)
# Don't worry about this button, just a place holder
self.button = Button(text="TAKE PICTURE")
self.layout.add_widget(self.button)
# Add a button
self.button2 = Button(text="SELECT")
self.layout.add_widget(self.button2) # Add a button
# Attach a callback for the button press event
self.button.bind(on_press=self.onButtonPress)
self.button2.bind(on_press=self.onButtonPress3)
return self.layout
#Place Holder don't worry about this either
def onButtonPress(self, button):
layout = GridLayout(cols=1, padding=10)
popupLabel = Label(text="TAKE PICTURE")
closeButton = Button(text="Close the pop-up")
layout.add_widget(popupLabel)
layout.add_widget(closeButton)
# Instantiate the modal popup and display
popup = Popup(title='TAKE PICTURE',
content=layout)
popup.open()
# Attach close button press
closeButton.bind(on_press=popup.dismiss)
def onButtonPress3(self, button):
layout = CheckBoxesApp()
popup3 = Popup(title='SELECT', content=layout)
popup3.open()
if __name__ == '__main__':
MyApp().run()
Checkboxes.kv
<Checkboxes>
# Create Box Layout
BoxLayout:
# set orientation and size
orientation: "vertical"
size: root.width, root.height
# Creating label
# create Grid Layout
GridLayout:
# Set number of column
cols:4
# create checkboxes
Label:
text: "Apple"
CheckBox:
# When check box selected, it will call checkboxes_click() method
on_active: root.checkboxes_click(self, self.active, "Apple")
Label:
text: "Bannana"
CheckBox:
on_active: root.checkboxes_click(self, self.active, "Bannana")
Label:
text: "Orange"
CheckBox:
on_active: root.checkboxes_click(self, self.active, "Orange")
Label:
text: "Grape"
CheckBox:
on_active: root.checkboxes_click(self, self.active, "Grape")
Label:
text: "Melon"
CheckBox:
on_active: root.checkboxes_click(self, self.active, "Melon")
Label:
text: "Peach"
CheckBox:
on_active: root.checkboxes_click(self, self.active, "Peach")
Label:
text: "Pineapple"
CheckBox:
on_active: root.checkboxes_click(self, self.active, "Pineapple")
Label:
text: "FINISH"
CheckBox:
on_active: root.checkboxes_click(self, self.active, "FINISH")
【问题讨论】:
【参考方案1】:Popup
的content
必须是Widget
,但App
不是Widget
,所以你的代码:
def onButtonPress3(self, button):
layout = CheckBoxesApp()
popup3 = Popup(title='SELECT', content=layout)
popup3.open()
会失败。
我建议将该代码更改为:
def onButtonPress3(self, button):
# layout = CheckBoxesApp()
layout = Checkboxes()
popup3 = Popup(title='SELECT', content=layout)
popup3.open()
当然,您必须明确加载 Checkboxes.kv
文件才能使其工作。
【讨论】:
兄弟,非常感谢,我被这个问题困扰了好久以上是关于如何在 Kivy 中使用 Popup 运行复选框小部件?的主要内容,如果未能解决你的问题,请参考以下文章
在 Kivy 中,为啥 popup.dismiss() 不从内存中删除弹出窗口?