我有一个主屏幕和一个弹出窗口。
主屏幕上的button
从输入中接收其文本。
弹出窗口显示一个按钮,该按钮从主屏幕上的按钮接收文本,当按下弹出按钮时,该按钮上的文本将返回主屏幕上的按钮。
基本上,我希望文本在按下时在这些按钮之间来回反弹。
只要主屏幕是根窗口小部件,这对我就有效。添加屏幕管理器时,functions
不会更改文本,但是print
语句仍会运行。
我已经尝试通过应用class
引用主屏幕并运行该方法,这将运行打印语句,但不会更改按钮文本。我能够更改按钮文本的唯一方法是,将主屏幕设为根窗口小部件,然后调用on_press:root.my_func()
我需要多个屏幕,所以我认为这不是解决方案。
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
pop_list = []
class MainWindow(Screen):
my_input = ObjectProperty(None)
btn_text = ObjectProperty(None)
def write_button(self):
self.btn_text.text = self.my_input.text
self.my_input.text = ''
def write_main(self,_text):
self.btn_text.text = _text
def show_popup(self):
popup = MyPopup()
popupWindow = Popup(size_hint=None,None)
,content=popup,size=(600,600))
popupWindow.open()
popup.write_pop()
def write_pop(self):
pop_list.append(self.btn_text.text)
self.btn_text.text = ''
class MyPopup(FloatLayout):
pop_btn = ObjectProperty(None)
def write_pop(self):
try:
self.pop_btn.text = pop_list[0]
except:
pass
class SecondWindow(Screen):
pass
class ScreenManager(ScreenManager):
pass
kv = Builder.load_file('popup_test.kv')
class PopUpTestApp(App):
mw = MainWindow()
def build(self):
return kv
if __name__ == '__main__':
PopUpTestApp().run()
还有kv文件
<Button>:
font_size: 10
color:0.3,0.4,0.5,1
size_hint: 0.5,0.1
<Label>:
font_size: 30
color:0,0,0,1
size_hint: 0.5,0.1
canvas.before:
Color:
rgb: 1,1,1,1
Rectangle:
pos: self.pos
size:self.size
ScreenManager:
MainWindow:
SecondWindow:
<MainWindow>:
name: 'main'
my_input:my_input
btn_text:btn_text
Label:
text: 'Input'
pos_hint: {'x':0, 'top':1}
TextInput:
id: my_input
size_hint: 0.5,0.1
pos_hint: {'x':0, 'top':0.9}
multiline:False
font_size:30
Label:
text: 'write to popup button'
pos_hint: {'right':1, 'y':0.9}
Button:
text:'write to mainscreen button'
pos_hint: {'x':0.,'top':0.8}
on_press: root.write_button()
Button:
id: btn_text
pos_hint: {'right':1.,'y':0.8}
on_press: root.write_pop()
Button:
text:'show popup'
pos_hint: {'right':1,'y':0.4}
on_press: root.show_popup()
<MyPopup>:
id:pop
pop_btn:pop_btn
Label:
text:'press to write to mainscreen button'
size_hint: 1,0.1
pos_hint: {'x': 0, 'top':1}
Button:
id:pop_btn
pos_hint: {'x': 0, 'top':0.9}
size_hint: 1,0.1
on_press:app.mw.write_main(self.text)
on_release:self.text = ''