[英]Kivy: Popup can have only one widget as content (When I import two different popup modules)
I am designing an app using kivy where I need two different Popup Widgets.我正在使用 kivy 设计一个应用程序,我需要两个不同的弹出窗口小部件。 I designed them using 'kv' language.
我使用“kv”语言设计它们。 When I import these Popup modules in my project file, It throws:
当我在我的项目文件中导入这些 Popup 模块时,它抛出:
'Popup can have only one widget as content'
'弹出窗口只能有一个小部件作为内容'
Here is my project file.这是我的项目文件。
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from myPopups.Popup1 import Popup1Window
from myPopups.Popup2 import Popup2Window
Builder.load_file('project.kv')
class ProjectWindow(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.mypopup1 = Popup1Window()
self.mypopup2 = Popup2Window()
Popup1.kv file popup1.kv 文件
<Popup>:
size_hint: 0.4,0.5
auto_dismiss:True
title: 'Popup 1'
id:mypopup1
orientation: "vertical"
BoxLayout:
id: header1
orientation:'vertical'
size_hint_y:1
size_hint_x:1
padding:3
canvas.before:
Color:
rgba:(.06,.35,.45,1)
BoxLayout:
id:text_box1
size_hint_y:None
size_hint_x:1
height:60
TextInput:
id:txt_qty1
text:''
multiline:False
halign: 'center'
valign: 'middle'
font_size:30
GridLayout:
id:popup_numpad1
size_hint_y:None
size_hint_x:1
# spacing:'3'
height:230
rows:4
cols:3
spacing:4
Button:
text: 'Popup 1 Button'
bold:True
font_size:17
background_normal:''
background_color:(.06,.45,.45,1)
Popup1.py file Popup1.py 文件
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
Builder.load_file('myPopus/popup1.kv')
class Popup1Window(Popup):
def __init__(self, **kwargs):
super().__init__(**kwargs)
print("This is Popup 1")
Similarly, Popup2.kv file同样,Popup2.kv 文件
<Popup>:
size_hint: 0.4,0.5
auto_dismiss:True
title: 'Popup 2'
id:mypopup2
orientation: "vertical"
BoxLayout:
id: header2
orientation:'vertical'
size_hint_y:1
size_hint_x:1
padding:3
canvas.before:
Color:
rgba:(.06,.35,.45,1)
BoxLayout:
id:text_box2
size_hint_y:None
size_hint_x:1
height:60
TextInput:
id:txt_qty2
text:''
multiline:False
halign: 'center'
valign: 'middle'
font_size:30
GridLayout:
id:popup_numpad2
size_hint_y:None
size_hint_x:1
# spacing:'3'
height:230
rows:4
cols:3
spacing:4
Button:
text: 'Popup 2 Button'
bold:True
font_size:17
background_normal:''
background_color:(.06,.45,.45,1)
Popup2.py file Popup2.py 文件
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
Builder.load_file('myPopus/popup2.kv')
class Popup2Window(Popup):
def __init__(self, **kwargs):
super().__init__(**kwargs)
print("This is Popup 2")
Please help me in this regard.请在这方面帮助我。 Thanks.
谢谢。
Your kv
files each contain a rule for <Popup>
.每个
kv
文件都包含一个<Popup>
规则。 When the second kv
file is loaded, the contents of the second <Popup>
rule are added to the first, resulting in a final rule for <Popup>
that has two children for the Popup
.当加载第二个
kv
文件时,第二个<Popup>
规则的内容被添加到第一个,从而导致<Popup>
的最终规则有两个子项用于Popup
。 When you load a rule for a class (like <Popup>
) it applies to all instances of Popup
that are created in your App
after loading the kv
.当您为类加载规则(如
<Popup>
)时,它适用于加载kv
后在您的App
中创建的所有Popup
实例。 Generally if you want different rules to apply to different instances of Popup
, you would use subclasses of Popup
(as you have), and load different rules for each subclass.通常,如果您希望将不同的规则应用于不同的
Popup
实例,您可以使用Popup
子类(如您Popup
),并为每个子类加载不同的规则。
You probably want to change the rule in Popup1.kv
to <Popup1Window>
and similarly, in Popup2.kv
change the rule to <Popup2Window>
.您可能希望将
Popup1.kv
的规则Popup1.kv
为<Popup1Window>
,类似地,将Popup2.kv
的规则更改为<Popup2Window>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.