问题描述
我试图将结果添加到内容应该是动态的新屏幕。我尝试了 StackOverflow 本身给出的一些琐碎方法,但未能显示任何内容。我的代码就是这样。
我正在输入多个数据,用“,”分隔,没有空格,使用文本输入,然后将其拆分为列表。数据将被解析,并且我尝试过但未成功执行的输出屏幕上应显示相同数量的标签(anbs.py 中的第 51-56 行)。
例如,输入是“I,am,a,good,boy”。
结果应该与控制台中的一样,即每个 Label 的文本应该包含一个单独的项目,但没有任何内容进入输出屏幕......只是一个用于在屏幕之间遍历的大按钮。
这是我的 anbs.py 文件。
from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.factory import Factory
Window.size = (600,600)
class HelpWindow(Screen):
"""
This is a help window. It contains the functionality of all the given Boxes
on the main window.
"""
def main_window(self):
sm.current = "main"
class MainWindow(Screen):
"""
This is the main window that contains the main form.
This connects the frontend of the app to the backend
"""
target = ObjectProperty(None)
def v_popup(self):
version_popup()
def help(self):
sm.current = "help"
def output_window(self):
sm.current = "output"
def get_results(self):
#Outputwindow.main(options = options)
out_window = Outputwindow()
out_window.main(self.target.text)
sm.current = "output"
class Outputwindow(Screen):
"""
This is the output window. All the generated results will be seen here.
"""
res = ObjectProperty(None)
res_out = ObjectProperty(None)
def main(self,options):
options = list(options.split(","))
for item in options:
print(item)
self.res_out.add_widget(Label(text=item))
def main_window(self):
sm.current = "main"
class WindowManager(ScreenManager):
pass
def version_popup():
"""
Version Popup Window.
"""
version = "v1.0"
version_text = "this is "+version+" for this app"
vpop = Popup(title="Version",content=Label(text=version_text),size_hint=(None,None),size=(400,400))
vpop.open()
### main builder and WindowManager object
kv = Builder.load_file("start.kv")
sm = WindowManager()
### Adding screens to widget
screens = [MainWindow(name="main"),HelpWindow(name="help"),Outputwindow(name="output")]
for screen in screens:
sm.add_widget(screen)
sm.current = "main"
### main working
class AnbsApp(App):
def build(self):
return sm
if __name__ == '__main__':
AnbsApp().run()
我的 start.kv 文件是这样的
<HelpWindow>
name:"help"
Button:
id:ms
text:"Main Screen"
on_release:
root.manager.transition.direction = "left"
root.main_window()
<MainWindow>
name:"main"
target : target
GridLayout:
cols:1
GridLayout:
cols:3
row_force_default: True
row_default_height: 50
Button:
id:hp
text:"Help"
on_release:
root.manager.transition.direction = "right"
root.help()
Button:
id:version
text: "Version"
on_release:
root.v_popup()
GridLayout:
cols:2
row_force_default: True
row_default_height: 30
Label:
text:"Target *"
TextInput:
id:target
multiline:False
GridLayout:
cols:3
row_force_default: True
row_default_height: 50
Label:
text:""
Button:
text:"Submit"
on_release:
root.get_results()
Label:
text:""
<Outputwindow>
name:"output"
res_out:res_out
GridLayout:
id:res_out
cols : 1
Button:
id:ms
text:"Main Screen"
on_release:
root.manager.transition.direction = "right"
root.main_window()
我真的无法说出我遗漏的主要观点是什么。
解决方法
您的代码:
def get_results(self):
#OutputWindow.main(options = options)
out_window = OutputWindow()
out_window.main(self.target.text)
sm.current = "output"
正在创建一个新的 OutputWindow
实例:
out_window = OutputWindow()
然后调用该新实例的 main()
方法。但是,该新实例不在您的 GUI 中,因此未观察到任何影响。要更正此问题,请更改代码以使用 GUI 中的 OutputWindow
实例:
def get_results(self):
out_window = self.manager.get_screen('output') # get OutputWindow instance
out_window.main(self.target.text)
sm.current = "output"
您还需要调整 Button
中 OutputWindow
的大小/位置,使其不会完全覆盖 GridLayout
。