Gavrk
Gavrk

Reputation: 325

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

I encountered some weird behavior of Kivy when trying to place the results of some analysis in a popup window of my App. By the button activation I can get a popup window (that should display the results of the analysis but is empty) and then my results are displayed in a debug window. But I'd like to see them in a popup. No error, no traceback, just weirdness.

This is how it looks like:

enter image description here

This is the line that runs a popup:

show_syllsoutput_popup()

This is the line that should populate it but populates debug window instead:

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

So, the question is how to put cfd_syll.tabulate() into this popup (.kv):

<SyllOutputPopup>:

    FloatLayout:

        Label:

            id: screen_output_label
            font_size: 12
            pos: 100, 120

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

Alternative test: if I try to populate output popup by this:

SyllOutputPopup.screen_output_label.text = cfd_syll.tabulate()

(without try:), I get AttributeError: type object 'SyllOutputPopup' has no attribute 'screen_output_label'

Here is the full traceback, in case it'd be helpful:

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'

Would be grateful for any help! I don't know what parts of code would be helpful, so I apologize in advance, any suggestions are greatly appreciated.

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

Additional info:

cfd_syll is defined in the fourth line of:

    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

Here is the Popup class:

class SyllOutputPopup(FloatLayout):
    pass

Here is definition of 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

In the .kv Popup is defined as:

<SyllOutputPopup>:

    FloatLayout:

        Label:

            id: screen_output_label
            font_size: 12
            pos: 100, 120

Upvotes: 0

Views: 213

Answers (1)

Lothric
Lothric

Reputation: 1318

screen_output_label is not the parameter of your class, it's widget's id, so that line

SyllOutputPopup.screen_output_label.text = cfd_syll.tabulate()

is wrong, you should use:

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

==========

Also you are creating several objects of SyllOutputPopup class. You put the text in one object:

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

And then you create new object, which is blank and has empty Label, and you show it:

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

You should use one object - set the text there and then show exactly that object, something like:

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

But that will work only if both of functions above are in the same class.

Upvotes: 1

Related Questions