Interacting with a gtk.container while gtk.main()

2019-05-07 20:14发布

问题:

Experimenting with a battery monitor icon at the moment in Python using pygtk and egg.trayicon to create an icon to display a battery icon/tooltip.

I seem to be able to add the icon and the tooltip text, but when it then reaches the gtk.main() stage I need a way to modify these so it can then show the updated values.

I've tried gobject.idle_add() and gobject.timeout_add() without much luck, not sure where to go from this.

Anyone got any ideas?

EDIT: Perhaps not the clearest of questions.

I need to loop, fetching information from acpi while running and apply it to widgets inside the gtk container.

EDIT 2: Ok, it's properly down now. The issue was that I wasn't returning anything inside my callback. I just gave it "return 123" and now it's happily chugging away in my system tray, notifying me of my battery percentage :)

回答1:

This example works for me:

# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4

import gobject
import gtk
from egg import trayicon

label = gtk.Label("Over here")

def callback(widget, ev):
    label.set_text("You found me")

def timeout():
    label.set_text("What are you waiting for?")

tray = trayicon.TrayIcon("TrayIcon")
box = gtk.EventBox()
box.add(label)
tray.add(box)
tray.show_all()

box.connect("button-press-event", callback)

gobject.timeout_add(3000L, timeout)

gtk.main()

Without seeing your code, it's hard to tell what doesn't work.



标签: python gtk pygtk