1

大家好,我一直在学习 python 和 pygtk 我最近遇到了一个我试图制作的小程序的问题。

当您第一次单击托盘图标时,我的窗口正常显示,但在关闭我试图设置为“最小化到托盘”类型的窗口后,它不会再次打开,只显示一个空白窗口而不是一个它最初显示。


    import os
    import gtk
    import gobject
    import datepicker
    from dateutil import relativedelta
    import datetime
    import numberentry
    class ShutdownTimer:
        tray_tooltip = ''
        timer_visible = False
        timerui = gtk.Window()
        def set_tooltip(self, string):
            self.tray_tooltip = str(string)
        def set_visible(self, v, visible):
        self.timer_visible = visible
        if self.timer_visible is True:
            self.timerui.show_all()
        else:
            self.timerui.hide_all()
        def set_up(self):
            self.timerui.set_title("Shutdown Timer")
            self.timerui.connect("destroy", self.set_visible, False)
            self.row_one = gtk.HBox()
            self.combo = gtk.combo_box_new_text()
            self.combo.append_text("Shutdown")
            self.combo.append_text("Hibernate")
            self.combo.append_text("Suspend/Sleep")
            self.combo.append_text("Restart")
            self.combo.append_text("Cancel")
            self.combo.set_active(0)
            self.row_one.pack_start(self.combo, False, False)

            hlbl = gtk.Label()
            mlbl = gtk.Label()
            hlbl.set_text("H:")
            mlbl.set_text("M:")

            self.hentry = numberentry.NumberEntry()
            self.mentry = numberentry.NumberEntry()
            submit = gtk.Button("Submit")
            submit.connect("clicked", self.submit_action)

            self.row_one.pack_start(hlbl, False, False)
            self.row_one.pack_start(self.hentry, False, False)
            self.row_one.pack_start(mlbl, False, False)
            self.row_one.pack_start(self.mentry, False, False)
            self.row_one.pack_start(submit, False, False)

            self.row_one.show_all()

            self.timerui.add(self.row_one)


        def submit_action(self, action):
            task = self.combo.get_active_text()
            hours = int(self.hentry.get_text())
            minus = int(self.mentry.get_text())
            hourm = hours * 60
            tmins = minus + hourm
            tseconds = tmins * 60
            print tseconds

            date = datetime.datetime.now()
            print date

            future = datetime.timedelta(seconds=tseconds)
            total = date+future
            print total

            print "%s scheduled for %s" % (task, total)
            string = task + " scheduled for " + str(total)
            md = gtk.MessageDialog(self.timerui, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE, string)
            md.run()
            md.destroy()
        #print '%s - %s / %i:%i %s' % (action, date, hours, minutes, timePart)
        #date_split = date.split("/")
        #today = datetime.date.today(date_split[2],date_split[0], date_split[1])
        #rd = relativedelta(today, datetime.date())
        #print "Seconds to go: %(seconds)d" % rd.__dict__
        def on_right_click(self, shutdown, status, action):
            menu = gtk.Menu()

            menu_item = gtk.MenuItem("Quit")
            menu_item.connect("activate", lambda w: gtk.main_quit())
            menu.append(menu_item)
            menu_item.show()
            menu_item = gtk.MenuItem("Show Window")
            menu_item.connect("activate", self.set_visible, True)
            menu.append(menu_item)
            menu_item.show()
            menu.popup(None, None, None, action, action)
        def __init__(self):
            self.status = gtk.StatusIcon()
            home = os.getenv('HOME')
            icon_path = home + '/.config/shutdowntimer/icons/32x32/tray_icon.png'
            settings_path = home + '/.config/shutdowntimer/settings/'
            self.status.set_from_file(icon_path)
            self.status.set_visible(True)
            self.status.connect("popup_menu", self.on_right_click)
            self.status.connect("activate", self.set_visible, True)
            self.set_up()

    def main():
        gtk.main()
        return 0

    if __name__ == "__main__":
    ShutdownTimer()
    main()

我确信我的代码在格式化、注释和可能的命名约定方面存在一些问题,但任何有助于阐明我的问题的帮助将不胜感激。谢谢:)!

4

1 回答 1

1

很抱歉浪费任何人的时间我设法通过在这个特定位置调用 self.set_up() 来解决我的问题

 def set_visible(self, v, visible):
    self.timer_visible = visible
    if self.timer_visible is True:
        self.set_up()
        self.timerui.show_all()
    else:
        self.timerui.hide_all()
于 2012-12-17T04:51:32.517 回答