我有pygtk的问题

时间:2012-12-17 00:43:19

标签: python pygtk

大家好,我一直在学习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()

我确信我的代码中存在一些问题,就格式化,评论和可能的命名约定而言,但任何有助于阐明我的问题的帮助都会受到赞赏..谢谢:)!

1 个答案:

答案 0 :(得分: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()