如何解决可以使用按钮在Kivy中向屏幕添加另一个标签吗
我希望弹出窗口中的最后一个按钮向上一个屏幕添加标签,并在弹出窗口的文本输入中输入任何内容,但我找不到办法了,这有可能吗? / p>
我希望ID为id的按钮每次单击时都添加一个标签以显示列表,并且该标签的文本应为在ids带有lab,club和blub的文本输入中输入的任何值
如果可能的话,我该怎么做,任何帮助将不胜感激。
Python:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import Screen,ScreenManager
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.uix.popup import Popup
import time
from kivy.properties import StringProperty
class Enter_Name(Screen):
input_1 = StringProperty()
def line(self):
App.get_running_app().root.get_screen('list').lab_text = self.airline.text
pass
class Pop(Screen):
air_craft = StringProperty()
def lad(self):
plane = App.get_running_app().root.get_screen('pop').lab_text = self.airplane.text
self.plane = plane
class List(Screen):
Enter_Name.line
def add(self):
show_popup()
def show_popup():
show = Pop()
pop_up_window = Popup(title="Add Route",content=show,size_hint=(None,None),size=(400,400))
pop_up_window.open()
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("pot.kv")
class am4(App):
def build(self):
return kv
if __name__ == "__main__":
am4().run()
基维:
#:kivy 1.0
WindowManager:
Enter_Name
List
Pop
#_______________________________________________________________________________________________________
#LOGIN
#_______________________________________________________________________________________________________
<Enter_Name>
airline: input_1
name: 'enter_name'
id: enter_nom
FloatLayout:
cols: 3
size: root.size
Label:
text: "Name of Airline?"
size_hint: 1,0.3
pos_hint: {"x": 0,"top":1}
TextInput:
multiline: False
name: 'input_one'
id: input_1
size_hint: 0.6,0.06
pos_hint: {"x": 0.20,"top":0.6}
Button:
size_hint: 0.2,0.1
pos_hint: {"x": 0.4,"top":0.4}
text: "Enter"
on_release:
app.root.current = 'list'
root.line()
#_______________________________________________________________________________________________________
#MAIN
#_______________________________________________________________________________________________________
<List>
lab_text: ''
name: 'list'
FloatLayout:
Label:
text: root.lab_text
size_hint: 1,0.3
pos_hint: {"x": -0.38,"top":1.1}
font_size: 50
Label:
text: '--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'
size_hint: 1,"top":1}
font_size: 50
Button:
text: "Add Route"
size_hint: 0.2,0.1
pos_hint: {"x":0.79,"top":0.99}
on_release: root.add()
<Pop>
name: "pop"
airplane: air_craft
FloatLayout:
Label:
id: lab
text: "Aircraft"
pos_hint: {"x": -0.38,"top":1.45}
Label:
id: club
text: "Departure"
pos_hint: {"x": 0,"top":1.45}
Label:
id: blub
text: "Arrival"
pos_hint: {"x": 0.38,"top":1.45}
TextInput:
multiline: False
name: 'aircraft'
id: air_craft
size_hint: 0.23,0.06
pos_hint: {"x": 0,"top":0.9}
TextInput:
multiline: False
name: 'departure'
id: leaving
size_hint: 0.23,0.06
pos_hint: {"x": 0.38,"top":0.9}
TextInput:
multiline: False
name: 'arrival'
id: arriving
size_hint: 0.23,0.06
pos_hint: {"x": 0.76,"top":0.9}
Button:
size_hint: 0.2,"top":0.5}
id: add
text: "Add"
on_release:
root.lad()
解决方法
这是您的代码的修改版本,可以满足我的要求:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import Screen,ScreenManager
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.properties import StringProperty
class Enter_Name(Screen):
input_1 = StringProperty()
def line(self):
App.get_running_app().root.get_screen('list').lab_text = self.airline.text
class ListRow(BoxLayout):
ac = StringProperty('')
arr = StringProperty('')
lv = StringProperty('')
class Pop(RelativeLayout):
def lad(self,ac,arr,lv):
list_scr = App.get_running_app().root.get_screen('list')
if ac != '' or arr != '' or lv != '':
box_layout = list_scr.ids.box_layout
box_layout.add_widget(ListRow(ac=ac,arr=arr,lv=lv))
list_scr.pop.dismiss()
class List(Screen):
Enter_Name.line
pop = ObjectProperty(None)
def add(self):
self.pop = show_popup()
def show_popup():
show = Pop()
pop_up_window = Popup(title="Add Route",content=show,size_hint=(None,None),size=(400,400))
pop_up_window.open()
return pop_up_window
class WindowManager(ScreenManager):
pass
# kv = Builder.load_file("pot.kv")
kv = Builder.load_string('''
#:kivy 1.0
WindowManager:
Enter_Name
List
#_______________________________________________________________________________________________________
#LOGIN
#_______________________________________________________________________________________________________
<Enter_Name>
airline: input_1
name: 'enter_name'
id: enter_nom
FloatLayout:
cols: 3
size: root.size
Label:
text: "Name of Airline?"
size_hint: 1,0.3
pos_hint: {"x": 0,"top":1}
TextInput:
multiline: False
name: 'input_one'
id: input_1
size_hint: 0.6,0.06
pos_hint: {"x": 0.20,"top":0.6}
Button:
size_hint: 0.2,0.1
pos_hint: {"x": 0.4,"top":0.4}
text: "Enter"
on_release:
app.root.current = 'list'
root.line()
#_______________________________________________________________________________________________________
#MAIN
#_______________________________________________________________________________________________________
<List>
lab_text: ''
name: 'list'
BoxLayout:
id: box_layout
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height
pos_hint: {'top':1}
BoxLayout:
orientation: 'vertical'
size_hint: 1,None
height: 50
BoxLayout:
orientation: 'horizontal'
Label:
id: lab1
text: root.lab_text
size_hint_x: 1
#pos_hint: {"x": -0.38,"top":1}
font_size: 50
Button:
text: "Add Route"
size_hint_x: 0.5
#pos_hint: {"x":0.79,"top":1}
on_release: root.add()
Label:
text: '--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'
size_hint: 1,0.3
#pos_hint: {"x": -0.38,"top":0.99}
font_size: 50
<ListRow>:
orientation: 'horizontal'
size_hint: (1,None)
height: self.minimum_height
Label:
text: 'Aircraft: ' + root.ac
halign: 'left'
size_hint: 1,None
height: self.texture_size[1]
Label:
text: 'Leave: ' + root.lv
halign: 'left'
size_hint: 1,None
height: self.texture_size[1]
Label:
text: 'Arrive: ' + root.arr
halign: 'left'
size_hint: 1,None
height: self.texture_size[1]
<Pop>
airplane: air_craft
FloatLayout:
Label:
id: lab
text: "Aircraft"
pos_hint: {"x": -0.38,"top":1.45}
Label:
id: club
text: "Departure"
pos_hint: {"x": 0,"top":1.45}
Label:
id: blub
text: "Arrival"
pos_hint: {"x": 0.38,"top":1.45}
TextInput:
multiline: False
name: 'aircraft'
id: air_craft
size_hint: 0.23,0.06
pos_hint: {"x": 0,"top":0.9}
TextInput:
multiline: False
name: 'departure'
id: leaving
size_hint: 0.23,0.06
pos_hint: {"x": 0.38,"top":0.9}
TextInput:
multiline: False
name: 'arrival'
id: arriving
size_hint: 0.23,0.06
pos_hint: {"x": 0.76,"top":0.9}
Button:
size_hint: 0.2,"top":0.5}
id: add
text: "Add"
on_release:
root.lad(air_craft.text,arriving.text,leaving.text)
''')
class am4(App):
def build(self):
return kv
if __name__ == "__main__":
am4().run()
我进行的一些重大更改:
- 将
Pop
类重新定义为扩展RelativeLayout
而不是Screen
,并从Pop
的子级中删除WindowManager
。 - 定义了一个
ListRow
类,该类在调用List
方法时被添加到Screen
lad()
中。 - 在
lad()
方法中添加了参数以添加信息,并添加了dismiss()
调用以关闭Popup
。 -
show_pop()
方法现在返回创建的Popup
实例,该实例保存在List
Screen
中以供dismiss()
使用。 - 使用垂直的
List
重新设计了Screen
BoxLayout
,以简化添加ListRows
的过程。
在上面的代码中使用kv
加载了Builder.load_string()
,但这只是为了我自己的方便。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。