在调试 window 中显示的文本而不是 Kivy 应用程序的弹出窗口 window

Text displayed in debug window instead of popup window of Kivy App

我在尝试将某些分析结果放入我的应用程序的弹出窗口 window 时遇到了 Kivy 的一些奇怪行为。通过按钮激活,我可以获得一个弹出窗口 window(应该显示分析结果但为空),然后我的结果显示在调试 window 中。但我想在弹出窗口中看到它们。没有错误,没有回溯,只有怪异。

这是它的样子:

这是运行弹出窗口的行:

show_syllsoutput_popup()

这是应该填充它的行,但却填充了 debug window:

try: SyllOutputPopup.screen_output_label.text = cfd_syll.tabulate()

因此,问题是如何将 cfd_syll.tabulate() 放入此弹出窗口 (.kv) 中:

<SyllOutputPopup>:

    FloatLayout:

        Label:

            id: screen_output_label
            font_size: 12
            pos: 100, 120

============================================= ================

替代测试:如果我尝试通过此填充输出弹出窗口:

SyllOutputPopup.screen_output_label.text = cfd_syll.tabulate()

(没有 try:),我得到 AttributeError: type object 'SyllOutputPopup' has no attribute 'screen_output_label'

这是完整的追溯,以防万一:

File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\runpy.py", line 193, in _run_module_as_main
  "__main__", mod_spec)
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\runpy.py", line 85, in _run_code
  exec(code, run_globals)
File "C:\GUI Projects\gercort\main.py", line 190, in <module>
  Gercort().run()
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\app.py", line 855, in run
  runTouchApp()
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\base.py", line 504, in runTouchApp
  EventLoop.window.mainloop()
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\core\window\window_sdl2.py", line 747, in mainloop
  self._mainloop()
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\core\window\window_sdl2.py", line 479, in _mainloop
  EventLoop.idle()
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\base.py", line 342, in idle
  self.dispatch_input()
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\base.py", line 327, in dispatch_input
  post_dispatch_input(*pop(0))
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\base.py", line 293, in post_dispatch_input
  wid.dispatch('on_touch_up', me)
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\_event.cp37-win32.pyd", line 707, in kivy._event.EventDispatcher.dispatch
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\uix\behaviors\button.py", line 179, in on_touch_up
  self.dispatch('on_release')
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\_event.cp37-win32.pyd", line 703, in kivy._event.EventDispatcher.dispatch
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\_event.cp37-win32.pyd", line 1214, in kivy._event.EventObservers.dispatch
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\_event.cp37-win32.pyd", line 1098, in kivy._event.EventObservers._dispatch
File "C:\Users\gavrk\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\kivy\lang\builder.py", line 64, in custom_callback
  exec(__kvlang__.co_value, idmap)
File "C:\GUI Projects\gercort\gercort.kv", line 484, in <module>
  on_release: root.output_toscreen(root.filepath)
File "C:\GUI Projects\gercort\main.py", line 135, in output_toscreen
  SyllOutputPopup.screen_output_label.text = cfd_syll.tabulate() # populate the popup

builtins.AttributeError: type object 'SyllOutputPopup' has no attribute 'screen_output_label'

如有任何帮助,我们将不胜感激!我不知道代码的哪些部分会有帮助,所以我提前道歉,非常感谢任何建议。

============================================= =====================

附加信息:

cfd_syll定义在第四行:

    def output_toscreen(сorpus_root, *args):

        corpus = PlaintextCorpusReader(args[0], '.*')
        cfd_syll = nltk.ConditionalFreqDist(
            (textname, num_syll)
            for textname in corpus.fileids()
            for num_syll in [len(w) for w in ''.join(char for char in reduce_dip(corpus.raw(fileids=textname)) if char in vowels).split()])

        show_syllsoutput_popup() # run the popup

        try: SyllOutputPopup.screen_output_label.text = cfd_syll.tabulate() # populate the popup

        except: pass

这是弹出窗口 class:

class SyllOutputPopup(FloatLayout):
    pass

这里是 show_syllsoutput_popup 的定义:

def show_syllsoutput_popup():
    show = SyllOutputPopup() # Create a new instance of the LicensePopup class 

    SyllOutputPopupWindow = Popup(title="Output", content=show, size_hint=(None,None),size=(600,400)) 
    # Create the popup window

    SyllOutputPopupWindow.open() # show the popup

在 .kv Popup 中定义为:

<SyllOutputPopup>:

    FloatLayout:

        Label:

            id: screen_output_label
            font_size: 12
            pos: 100, 120

screen_output_label 不是你的 class 的参数,它是小部件的 id,所以那行

SyllOutputPopup.screen_output_label.text = cfd_syll.tabulate()

是错误的,你应该使用:

SyllOutputPopup.ids.screen_output_label.text = cfd_syll.tabulate()

==========

您还创建了多个 SyllOutputPopup 对象 class。您将文本放在一个对象中:

try: SyllOutputPopup.ids.screen_output_label.text = cfd_syll.tabulate()

然后创建新对象,它是空白的且具有空标签,并显示它:

show = SyllOutputPopup() # Create a new instance
    SyllOutputPopupWindow = Popup(title="Output", content=show, size_hint=(None,None),size=(600,400)) 
    SyllOutputPopupWindow.open()

您应该使用一个对象 - 在那里设置文本,然后准确显示该对象,例如:

    def output_toscreen(сorpus_root, *args):

        corpus = PlaintextCorpusReader(args[0], '.*')
        cfd_syll = nltk.ConditionalFreqDist(
            (textname, num_syll)
            for textname in corpus.fileids()
            for num_syll in [len(w) for w in ''.join(char for char in reduce_dip(corpus.raw(fileids=textname)) if char in vowels).split()])
        # that will be your object
        self.sylloutputpopup = SyllOutputPopup()
        self.sylloutputpopup.ids.screen_output_label.text = cfd_syll.tabulate()
        show_syllsoutput_popup() # run the popup

def show_syllsoutput_popup():
    show = self.sylloutputpopup
    SyllOutputPopupWindow = Popup(title="Output", content=show, size_hint=(None,None),size=(600,400)) 
    SyllOutputPopupWindow.open()

但这只有在上述两个函数都在同一个 class 中时才有效。