Python 3.x Kivy:Popup只能有一个小部件作为内容
我在.kv文件中使用弹出窗口时遇到问题。我知道一个弹出窗口只能有一个小部件作为它的内容,但是如果我只是作为一个孩子传递一个GridLayout,其中包括一个标签和按钮,这不应该工作吗 以下是我的Python代码:Python 3.x Kivy:Popup只能有一个小部件作为内容,python-3.x,popup,kivy,kivy-language,Python 3.x,Popup,Kivy,Kivy Language,我在.kv文件中使用弹出窗口时遇到问题。我知道一个弹出窗口只能有一个小部件作为它的内容,但是如果我只是作为一个孩子传递一个GridLayout,其中包括一个标签和按钮,这不应该工作吗 以下是我的Python代码: import kivy, LabelB from kivy.app import App from kivy.graphics import Color, Rectangle from kivy.uix.boxlayout import BoxLayout from kivy.uix.
import kivy, LabelB
from kivy.app import App
from kivy.graphics import Color, Rectangle
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.properties import ObjectProperty, StringProperty
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
Builder.load_file('main.kv')
class CustomPopup(Popup):
pass
class MenuScreen(Screen):
def open_popup(self):
the_popup = CustomPopup()
the_popup.open()
class SurveyScreen(Screen):
pass
sm = ScreenManager(transition=FadeTransition())
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SurveyScreen(name='survey'))
class MainApp(App):
def build(self):
return sm
if __name__ == '__main__':
MainApp().run()
这是我的.kv文件:
<CustomPopup>:
title: 'Terms of Service'
size_hint: .5, .5
auto_dismiss: False
GridLayout:
cols: 1
Label:
size_hint: .9, .9
halign: 'center'
valign: 'middle'
text: 'Insert terms of service text here'
text_size: self.width, None
Button:
text: 'Close'
on_release: root.dismiss()
<MenuScreen>:
FloatLayout:
canvas.before:
Rectangle:
source: 'menu.png'
size: self.size
pos: self.pos
Label:
pos_hint: {'x': .7, 'y': .85}
text_size: self.size
font_name: 'Arial'
font_size: 26
text: 'Sample'
bold: True
Button:
text: 'Take Survey'
size_hint: .2, .1
pos_hint: {'x': .15, 'y': .1}
on_release:
root.manager.transition.duration = 0.5
root.manager.current = 'survey'
Button:
text: 'Terms of Service'
size_hint: .2, .1
pos_hint: {'x': .6-self.size_hint_x, 'y': .1}
on_release: root.open_popup()
Button:
text: 'Quit'
size_hint: .2, .1
pos_hint: {'x': .85-self.size_hint_x, 'y': .1}
on_release: app.stop()
<SurveyScreen>:
GridLayout:
cols: 1
padding: 20
spacing: 10
Label:
text: 'WELCOME!'
font_size: 20
Label:
text: 'Some boring text'
:
标题:“服务条款”
大小提示:.5,.5
自动解除:错误
网格布局:
科尔斯:1
标签:
大小提示:.9,.9
哈利恩:“中心”
valign:“中间”
文本:“在此处插入服务条款文本”
文本大小:self.width,无
按钮:
文本:“关闭”
发布时:root.disclose()
:
浮动布局:
在以下情况之前:
矩形:
来源:“menu.png”
大小:self.size
pos:self.pos
标签:
位置提示:{'x':.7,'y':.85}
文本大小:self.size
字体名称:“Arial”
字体大小:26
文本:“示例”
黑体字:对
按钮:
文本:“进行调查”
大小提示:.2,.1
位置提示:{'x':.15,'y':.1}
发布时:
root.manager.transition.duration=0.5
root.manager.current='survey'
按钮:
正文:“服务条款”
大小提示:.2,.1
pos_hint:{'x':.6-self.size_hint_x,'y':.1}
发布时:root.open\u popup()
按钮:
文本:“退出”
大小提示:.2,.1
pos_hint:{'x':.85-self.size_hint_x,'y':.1}
发布时:app.stop()
:
网格布局:
科尔斯:1
填充:20
间距:10
标签:
文字:“欢迎!”
字体大小:20
标签:
文字:“一些无聊的文字”
错误如下:“弹出窗口只能有一个小部件作为内容”
我是不是漏掉了什么明显的东西?提前感谢。是的,它应该按照您所说的那样工作,您的代码是正确的 问题是.kv文件的加载是重复的。由于您的
App
子类被称为MainApp
,main.kv
在同一目录(Doc:)中时会自动加载。另一方面,您可以使用Builder.load_file('main.kv')
显式上传文件
您必须删除对Builder
的调用或重命名MainApp
/main.kv
如果删除对Builder.load\u文件的调用
,则必须在加载.kv后创建ScreenManager
实例。您可以执行以下操作:
class MainApp (App):
def build (self):
sm = ScreenManager (transition = FadeTransition ())
sm.add_widget (MenuScreen (name = 'menu'))
sm.add_widget (SurveyScreen (name = 'survey'))
return sm
我将app类的名称改为SampleApp,并保持Builder.load_file('main.kv')行不变,现在一切正常,谢谢!