【发布时间】:2019-10-16 20:55:29
【问题描述】:
我的屏幕有两个盒子布局。第一个是空的。第二个有打开弹出窗口的按钮。在弹出窗口中,我有 2 个文本输入和复选框。我也想将这些文本输入作为标签添加到该 boxlayout 和复选框中。
如果我在那个屏幕上有按钮,我可以将按钮添加到 boxlayout。但是一旦我们去弹出窗口,我就无法弄清楚该怎么做。
派。
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.screenmanager import Screen, ScreenManager
class MyPopup(Popup):
pass
class MainScreen(Screen):
pass
class SecondScreen(Screen):
def fire_popup(self):
pops = MyPopup()
pops.open()
class ScreenManagement(ScreenManager):
def changescreen(self, value):
try:
if value !='main':
self.current = value
except:
print('No Screen named'+ value)
class testiApp(App):
def build(self):
self.title = 'Hello'
def add_more(self):
addbutton = self.root.get_screen('Page2').ids.empty
addbutton.add_widget(Button(text='Hello'))
def remove(self):
container = self.root.get_screen('Page2').ids.empty
if len(container.children) > 0:
container.remove_widget(container.children[0])
testiApp().run()
千伏。
<MyPopup>:
id:pop
size_hint: .4, .4
auto_dismiss: False
title: 'XXX!!'
BoxLayout:
orientation:'vertical'
BoxLayout:
orientation:'horizontal'
Label:
text:'X1'
TextInput:
id: X1
Label:
text:'X2'
TextInput:
id:X2
CheckBox:
id:X3
BoxLayout:
orientation:'horizontal'
Button:
text:'Add'
on_release: app.add_more()
Button:
text: 'Close'
on_press: pop.dismiss()
ScreenManagement:
MainScreen:
name:'Main'
SecondScreen:
name:'Page2'
<MainScreen>:
name:'Main'
BoxLayout:
Button:
text:'Next Page'
on_release: app.root.current ='Page2'
<SecondScreen>:
name:'Page2'
BoxLayout:
orientation:'vertical'
BoxLayout:
orientation:'vertical'
Label:
text:'Popup Test'
ScrollView:
GridLayout:
orientation: "vertical"
size_hint_y: None
height: self.minimum_height
row_default_height: 60
cols:1
id:empty
BoxLayout:
Button:
text:'Open Popup'
on_press: root.fire_popup()
Button:
text:'Add'
on_release: app.add_more()
Button:
text:'remove'
on_release: app.remove()
【问题讨论】:
-
除了您的
add_more()方法中的text= root.错误,它似乎对我来说工作正常。也许不清楚你想要什么。 -
我在玩代码时不小心添加了那个 text=root。原来只是 text='hello'
标签: python python-3.x popup kivy