Kivy:弹出窗口中的多个项目无法工作
我正在尝试在Python的Kivy中使用多个项目在弹出窗口中显示。我想知道该怎么做。我不确定这是否与我的脚本在手机上运行而不是在电脑上有关。
import kivy
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
popup = Popup(title='Test popup',
content=Label(text='Hello world'),
TextInput(text='Hi'), #Here is what I am trying to make work
size_hint=(None, None), size=(400, 400))
你可以看到在弹出窗口的内容中有两个对象。我相信这是可能的,因为我在应用商店的Kivy应用中见过,但我自己不太清楚该怎么做。
3 个回答
0
你需要这样做:
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.super_box = BoxLayout(orientation = "vertical")
self.pn = TextInput(text = "projectname") # TODO: Project name input
self.super_box.add_widget(self.pn)
"""Create button: Creates a new project repo"""
cb = Button(text="Create")
cb.bind(on_release = lambda x: print("a new project repo gets created"))
self.super_box.add_widget(cb)
0
你可以通过使用 .kv 文件来实现这个功能。
<Content>:
orientation:'vertical'
Label:
text: 'Hello World'
Button:
text: 'Press Me'
在 Python 文件中:
def openPop(self):
self.pop = Popup(title='Test',content=Content(),auto_dismiss=True)
self.pop.open()
11
弹出窗口的内容只能放一个小部件。你不能像现在这样同时放两个小部件。
要实现你想要的效果,你需要把标签和文本输入框放到一个盒子布局里,然后再把这个盒子布局放到弹出窗口的内容里。下面是一个可以工作的例子:
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
box = BoxLayout()
box.add_widget(Label(text='Hello world'))
box.add_widget(TextInput(text='Hi'))
popup = Popup(title='Test popup', content=box, size_hint=(None, None), size=(400, 400))