0

For some reason the following code works fine but if computer not used for about 10 minutes (no mouse/keyboard interaction) then the ToolButton text stops being displayed on panel and is just blank. The strange thing is if using PC it will stay working for longer, any ideas why or what could cause this sort of inactivity fault?

Think my next step maybe to install a fresh Linux VM to test code in and rule out my install as running out of ideas of things to try.

will also have another go at trying to get code to start from terminal instead of the mate desktop "add to panel" or mate-panel-test-applets commands as i'm sure there are other errors/warnings not being printed via print statements due to being a panel applet.

#Example panel applet output text: "/ = 6G"

I'm Using: 
Linux katerina 4.15.0-36-generic #39-Ubuntu SMP Mon Sep 24 16:19:09 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
Ubuntu 18.04.1 LTS
GTK 3.22.30
python sys.version = '3.6.6 (default, Sep 12 2018, 18:26:19) \n[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]]
  • Know controls working as can get click events
  • Checked screensaver, Power managment options
  • Tried forceing redraw events on ToolButton
  • checked ToolButton lable.text updateing still
  • Checked my disk free code still working so know scheduler working
  • checked changing disk space text dd if=/dev/zero of=test bs=1024 count=1000000
  • Tried setting box.set_above_child(False)

    import sys
    import logging
    import logging.handlers
    import os
    import shutil
    import gi
    
    gi.require_version('MatePanelApplet', '4.0')
    gi.require_version("Gtk", "3.0")
    
    from gi.repository import Gtk
    from gi.repository import Gdk
    from gi.repository import MatePanelApplet
    from datetime import datetime
    from apscheduler.schedulers.background import BackgroundScheduler
    from hurry.filesize import size
    

    def configure_logger():
        #Create a new logger and connect to syslog as invokes as script
        my_logger = logging.getLogger("TestPythonApplet")
        my_logger.setLevel(logging.DEBUG)
        logging.getLogger('apscheduler').setLevel(logging.DEBUG)
        formatter = logging.Formatter('%(name)s[%(process)d]: %(levelname)s: %(message)s')
        handler = logging.handlers.SysLogHandler(address = '/dev/log')          
        handler.setFormatter(formatter)
        my_logger.addHandler(handler)
        return my_logger

<pre>

# create the applet. Basicly all your applet code for buttons, etc here
def applet_fill(applet):
    #todo: maybe create a applet class like dock_applet.py example
    my_logger.debug('Applet started')

    #NOTE: Click events not always registered when click using ToolButton
    #Think because it dont own its own X-window for preformance reasons as mostly decrotive
    #As such dont normaly have to handle x events
    #see: https://developer.gnome.org/gtkmm-tutorial/3.0/chapter-widgets-without-xwindows.html.en
    #So we wrap in an Gtk.EventBox()
    button = Gtk.ToolButton()

    box = Gtk.EventBox()    #Used to capture mouse events
    box.set_above_child(True)   #Fixes left clicks not being detected 100%

    box.add(button)
    box.connect("button_press_event", button_click, applet)
    applet.add(box)

    #    show_image = False



#    if show_image:
#        #Follow works for showing image 
#        theme = Gtk.IconTheme().get_default()
#        if theme.has_icon("mate"):      #Keep things simple and look for known icon installed
#            my_logger.debug('Mate icon found')
#
#            image = Gtk.Image()
#            #<class 'gi.repository.GdkPixbuf.Pixbuf
#            i = theme.load_icon("happy", 32, Gtk.IconLookupFlags.NO_SVG)     
#            s= image.new_from_pixbuf(i)    #<class 'gi.repository.Gtk.Image'> 
#            button.set_icon_widget(s)
#    else:   #Use text mode
#        button.set_label(get_disk_free())

<pre>
    button.set_label(get_disk_free())
    #Now finnished creating applet start disk
    scheduler.start()
    scheduler.add_job(set_disk_free, 'interval', args=[applet],  seconds=1, id='set_disk_free_id', replace_existing=True)    #Args to pass to function set_disk_free
    applet.show_all()
    return True
</pre>
        def set_disk_free(applet):
        #TODO: Is there a way to applet.get_child_by_name("GtkToolButton") type function
        #Find toolButton widget and set text    
        for items in applet.get_children():     # Get list of child objects under applet 
            #my_logger.debug(items.get_name())   # should be a 'gi.repository.Gtk.EventBox'
            if items.get_name() == "GtkEventBox":
                for items in items.get_children():
                    if items.get_name() == "GtkToolButton": #Found main tool button
                        items.set_label(get_disk_free())  
                        items.queue_draw()  #lets try forceing a redraw of widget for some reason
                                            #if dont touch pc for 10 min cant see text in pannel
                                            #yet the debug line below shows text is still set and updating
                        my_logger.debug('Label: \"' + items.get_label() + '\"')

def get_disk_free():
    usage_string = '/ = '+ size(shutil.disk_usage('/').free)
    my_logger.debug('Updateing:' +  str(datetime.now().strftime('%d-%m-%Y %H:%M:%S')) + usage_string )
    return usage_string

def button_click(widget, event, applet):
    if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 1:
        my_logger.debug('Left Button clicked')
        my_logger.debug('Updating free space text')
        set_disk_free(applet)

    if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 3:
        my_logger.debug('Right Button clicked')


def applet_factory(applet, iid, data):
# return False to tell mate we did not create a applet

    if iid != "TestPythonApplet":
       return False
    applet_fill(applet)
    return True

#Code starts here
my_logger = configure_logger()    
scheduler = BackgroundScheduler()

MatePanelApplet.Applet.factory_main("TestPythonAppletFactory", True,
                                    MatePanelApplet.Applet.__gtype__,
                                    applet_factory, None)
my_logger.debug('Exiting')
scheduler.shutdown()



<pre>
#
#To test:
##########
##Create File: org.mate.panel.applet.TestPythonApplet.service
#[D-BUS Service]
#Name=org.mate.panel.applet.TestPythonAppletFactory
#Exec=/usr/bin/env python3 /home/chiptoxic/Downloads/mate-panel-applet/test_python_applet/test_python_applet.py
#
##Create File: org.mate.panel.TestPythonApplet.mate-panel-applet
#[Applet Factory]
#Id=TestPythonAppletFactory
#InProcess=false
#Location=/home/chiptoxic/Downloads/mate-panel-applet/test_python_applet/test_python_applet.py
#Name=Test Applet Factory
#Description=A MATE Python Applet example factory
# 
#[TestPythonApplet]
#Name=Test Python Applet
#Description=A MATE Python Applet example
#Icon=happy
#MateComponentId=OAFIID:MATE_TestPythonApplet

#Simplified path to save space
#sudo ln -s org.mate.panel.TestPythonApplet.mate-panel-applet /usr/share/mate-panel/applets/org.mate.panel.TestPythonApplet.mate-panel-applet
#sudo ln -s org.mate.panel.applet.TestPythonApplet.service /usr/share/dbus-1/services/org.mate.panel.applet.TestPythonApplet.service
#chmod 755 org.mate.panel.TestPythonApplet.mate-panel-applet
#chmod 644 org.mate.panel.TestPythonApplet.service
#tail -f /var/log/syslog
# 
#Following will kill process id of applet (have as bash script binded to key for easy testing / reload
#kill $(ps -aux | grep python3 | grep test_python_applet.py | tr -s ' ' | cut -d ' ' -f 2)
#
################################################################################ 
</pre>
liberforce
  • 11,189
  • 37
  • 48
  • You are actually using Gtk3, not pygtk. Hint: its not the same. Pygtk is Gtk2. – theGtknerd Oct 22 '18 at 21:49
  • 1
    Please share a [MCVE](https://stackoverflow.com/help/mcve). Your code is a hodge podge of comments and who knows what, so I can't really test it. – theGtknerd Oct 22 '18 at 21:50
  • Thanks for your insights, I put pygtk as a tag as until you pointed it out i didn't realize fully the difference between PyGTK and PyGObject. Guess i confused myself as some of the online docs i read redirected straight to the new docs. Its only because i had use used PyGTK in the past years ago that my research started there. – chiptoxic Oct 24 '18 at 22:44
  • Regarding the code i tried to use HTML pre tags as site would not accept my pasted code for some reason even when trying to realign like it wanted. Ill try reposting if i'm still stuck by weekend. I may have narrowed the problem down to the event box as when i add just the ToolButton to panel seems to work and when inside the event box fails after some time. Ill post an update when i have time to confirm this. It takes awhile to test as bug only shows up after 10 minutes of inactivity. – chiptoxic Oct 24 '18 at 23:08

0 Answers0