在调试窗口而不是Kivy App的弹出窗口中显示的文本

时间:2020-02-17 04:09:59

标签: python debugging popup kivy

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

它是这样的:

enter image description here

这是运行弹出窗口的行:

show_syllsoutput_popup()

这是应该填充它的行,而是填充调试窗口:

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

这是Popup类:

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中的弹出窗口定义为:

<SyllOutputPopup>:

    FloatLayout:

        Label:

            id: screen_output_label
            font_size: 12
            pos: 100, 120

1 个答案:

答案 0 :(得分:1)

screen_output_label不是您的类的参数,它是小部件的ID,因此该行

SyllOutputPopup.screen_output_label.text = cfd_syll.tabulate()

是错误的,您应该使用:

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

==========

还要创建SyllOutputPopup类的几个对象。您将文本放在一个对象中:

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

然后创建一个新对象,该对象为空白并且具有空的Label,并显示它:

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()

但这仅在以上两个函数在同一类中时才有效。