Hey everyone,
I'm currently working on a small script that needs to use gtk.StatusIcon()
. For some reason, I'm getting some weird behavior with it. If I go into the python interactive shell and type:
>> import gtk
>> statusIcon = gtk.status_icon_new_from_file("img/lin_idle.png")
Pygtk does exactly what it should do, and shows an icon (lin_idle.png) in the system tray:
However, if I try to do the same task in my script:
def gtkInit(self): self.statusIcon = gtk.status_icon_new_from_file("img/lin_idle.png")
When gtkInit()
gets called, I see this instead:
I made I ran the script in the same working directory as the interactive python shell, so I'm pretty sure it's finding the image, so I'm stumped... Any ideas anyone? Thanks in advance.
Update: For some reason or another, after calling gtk.status_icon_new_from_file()
a few times in the script, it does eventually create the icon, but this issue still remains unfortunately. Does anyone at all have any ideas as to what could be going wrong?
As requested: Here's the full script. This is actually an application that I'm in the very early stages of making, but it does work at the moment if you get it setup correctly, so feel free to play around with it if you want (and also help me!), you just need to get an imgur developer key and put it in linup_control.py
Linup.py
# # Linup - A dropbox alternative for Linux! # Written by Nakedsteve # Released under the MIT License # import os import time import ConfigParser from linup_control import Linup cfg = ConfigParser.RawConfigParser() # See if we have a .linuprc file home = os.path.expanduser("~") if not os.path.exists(home+"/.linuprc"): # Nope, so let's make one cfg.add_section("paths") cfg.set("paths","watch_path", home+"/Desktop/screenshot1.png") # Now write it to the file with open(home+"/.linuprc","wb") as configfile: cfg.write(configfile) else: cfg.read(home+"/.linuprc") linup = Linup() # Create the GUI (status icon, menus, etc.) linup.gtkInit() # Enter the main loop, where we check to see if there's a shot to upload # every 1 second path = cfg.get("paths","watch_path") while 1: if(os.path.exists(path)): linup.uploadImage(path) url = linup.getURL() linup.toClipboard(url) linup.json = "" print "Screenshot uploaded!" os.remove(path) else: # If you're wondering why I'm using time.sleep() # it's because I found that without it, my CPU remained # at 50% at all times while running linup. If you have a better # method for doing this, please contact me about it (I'm relatively new at python) time.sleep(1)
linup_control.py
import gtk import json import time import pycurl import os class Linup: def __init__(self): self.json = "" def uploadImage(self, path): # Set the status icon to busy self.statusIcon.set_from_file("img/lin_busy.png") # Create new pycurl instance cu = pycurl.Curl() # Set the POST variables to the image and dev key vals = [ ("key","*************"), ("image", (cu.FORM_FILE, path)) ] # Set the URL to send to cu.setopt(cu.URL, "http://imgur.com/api/upload.json") # This lets us get the json returned by imgur cu.setopt(cu.WRITEFUNCTION, self.resp_callback) cu.setopt(cu.HTTPPOST, vals) # Do eet! cu.perform() cu.close() # Set the status icon to done... self.statusIcon.set_from_file("img/lin_done.png") # Wait 3 seconds time.sleep(3) # Set the icon to idle self.statusIcon.set_from_file("img/lin_idle.png") # Used for getting the response json from imgur def resp_callback(self, buff): self.json += buff # Extracts the image URL from the json data def getURL(self): js = json.loads(self.json) return js['rsp']['image']['original_image'] # Inserts the text variable into the clipboard def toClipboard(self, text): cb = gtk.Clipboard() cb.set_text(text) cb.store() # Initiates the GUI elements of Linup def gtkInit(self): self.statusIcon = gtk.StatusIcon() self.statusIcon.set_from_file("img/lin_idle.png")