2016-09-18 67 views
2

我試圖讓使用GTK時鐘在python 2.我的版本在這一點上只是一個基本的更新繪圖區域,這將是更加複雜,但是我需要使它先刷新。最後,它將成爲一個屏幕保護程序,希望。如何在GTK

我試圖弄明白queue_draw()是如何工作的,但我無法弄清楚。我是GTK的新手,並沒有使用類的經驗(儘管我正在嘗試)。這兩個是我想我的問題。 有人可以幫我這個部分,請:d 我知道有這個問題的帖子,我仍然無法弄清楚如何使這項工作:( 謝謝

我使用Ubuntu 14.04,蟒蛇2.

這是我目前所面對的代碼:

#!/usr/bin/env python 
import time 
import gtk 
import math 

class Base: 
    def destroy(self, widget, data=None): 
     gtk.main_quit() 

    def close(self, widget): 
     self.window.destroy() 

    def rotate(self, center, point, angle): 
     #http://stackoverflow.com/questions/12161277/how-to-rotate-a-vertex-around-a-certain-point 
     px = point[0] 
     py = point[1] 
     rx = center[0] + (px - center[0]) * math.cos(math.radians(angle)) - (py - center[1]) * math.sin(math.radians(angle)) 
     ry = center[1] + (px - center[0]) * math.sin(math.radians(angle)) + (py - center[1]) * math.cos(math.radians(angle)) 
     return(rx, ry) 

    # This function will be called whenever the drawing area is exposed: 
    def expose_handler(self, widget, event) : 
     w, h = widget.window.get_size() 
     center = [int(w/2), int(h/2)] 
     xgc = widget.window.new_gc() 
             #%H for a 24 hour system as %I is for a 12 hour system 
     TIME = time.strftime("%Y-%m-%d %I:%M:%S") #add this to get GMT time# , time.gmtime()) 
     H, M, S = TIME.split(" ")[1].split(":") 
     H, M, S = int(H), int(M), int(S) 

     Dial_radius = (h/2 - ((h/2)/10))/3 
     H_size, M_size, S_size = (Dial_radius/2), Dial_radius, Dial_radius 

     H_angle = H * (360/12) + (float(M)/60) * (360/12) 
     M_angle = M * (360/60) + (float(S)/60) * (360/60) 
     S_angle = S * (360/60) 

     H_zero = [center[0], center[1] - H_size] 
     M_zero = [center[0], center[1] - M_size] 
     S_zero = [center[0], center[1] - S_size] 

     Hx, Hy = self.rotate(center, H_zero, H_angle) 
     Mx, My = self.rotate(center, M_zero, M_angle) 
     Sx, Sy = self.rotate(center, S_zero, S_angle) 

     #Hour 
     xgc.set_rgb_fg_color(gtk.gdk.color_parse("red")) 
     widget.window.draw_line(xgc, center[0], center[1], int(Hx), int(Hy)) 

     #Minute 
     xgc.set_rgb_fg_color(gtk.gdk.color_parse("green")) 
     widget.window.draw_line(xgc, center[0], center[1], int(Mx), int(My)) 

     #Second 
     xgc.set_rgb_fg_color(gtk.gdk.color_parse("blue")) 
     widget.window.draw_line(xgc, center[0], center[1], int(Sx), int(Sy)) 



    def __init__(self): 
     self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) 
     self.window.set_position(gtk.WIN_POS_CENTER) 
     #self.window.fullscreen() 
     self.window.set_size_request(600, 400) 

     self.vbox = gtk.VBox() 
     self.window.add(self.vbox) 

     # Create an area to draw in: 
     self.drawing_area = gtk.DrawingArea() 
     self.vbox.pack_start(self.drawing_area) 
     self.drawing_area.show() 

     self.drawing_area.connect("expose-event", self.expose_handler) 
     self.drawing_area.show() 
     self.window.show_all() 
     self.window.connect("destroy", self.destroy) 

    def main(self): 
     gtk.main() 

if __name__ == "__main__": 
    base = Base() 
    base.main() 

我已經與acw1668的代碼更新代碼,現在它的工作,再次感謝,它看起來像這樣:

#!/usr/bin/env python 
import time 
import gtk 
import math 
import glib 

class Base: 
    def destroy(self, widget, data=None): 
     gtk.main_quit() 

    def close(self, widget): 
     self.window.destroy() 

    def refresh_clock(self): 
     self.drawing_area.queue_draw() 
     return True 

    def rotate(self, center, point, angle): 
     #http://stackoverflow.com/questions/12161277/how-to-rotate-a-vertex-around-a-certain-point 
     px = point[0] 
     py = point[1] 
     rx = center[0] + (px - center[0]) * math.cos(math.radians(angle)) - (py - center[1]) * math.sin(math.radians(angle)) 
     ry = center[1] + (px - center[0]) * math.sin(math.radians(angle)) + (py - center[1]) * math.cos(math.radians(angle)) 
     return(rx, ry) 

    # This function will be called whenever the drawing area is exposed: 
    def expose_handler(self, widget, event) : 
     w, h = widget.window.get_size() 
     center = [int(w/2), int(h/2)] 
     xgc = widget.window.new_gc() 
             #%H for a 24 hour system as %I is for a 12 hour system 
     TIME = time.strftime("%Y-%m-%d %I:%M:%S") #add this to get GMT time# , time.gmtime()) 
     H, M, S = TIME.split(" ")[1].split(":") 
     H, M, S = int(H), int(M), int(S) 

     Dial_radius = (h/2 - ((h/2)/10))/3 
     H_size, M_size, S_size = (Dial_radius/2), Dial_radius, Dial_radius 

     H_angle = H * (360/12) + (float(M)/60) * (360/12) 
     M_angle = M * (360/60) + (float(S)/60) * (360/60) 
     S_angle = S * (360/60) 

     H_zero = [center[0], center[1] - H_size] 
     M_zero = [center[0], center[1] - M_size] 
     S_zero = [center[0], center[1] - S_size] 

     Hx, Hy = self.rotate(center, H_zero, H_angle) 
     Mx, My = self.rotate(center, M_zero, M_angle) 
     Sx, Sy = self.rotate(center, S_zero, S_angle) 

     #Hour 
     xgc.set_rgb_fg_color(gtk.gdk.color_parse("red")) 
     widget.window.draw_line(xgc, center[0], center[1], int(Hx), int(Hy)) 

     #Minute 
     xgc.set_rgb_fg_color(gtk.gdk.color_parse("green")) 
     widget.window.draw_line(xgc, center[0], center[1], int(Mx), int(My)) 

     #Second 
     xgc.set_rgb_fg_color(gtk.gdk.color_parse("blue")) 
     widget.window.draw_line(xgc, center[0], center[1], int(Sx), int(Sy)) 



    def __init__(self): 
     self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) 
     self.window.set_position(gtk.WIN_POS_CENTER) 
     #self.window.fullscreen() 
     self.window.set_size_request(600, 400) 

     self.vbox = gtk.VBox() 
     self.window.add(self.vbox) 

     # Create an area to draw in: 
     self.drawing_area = gtk.DrawingArea() 
     self.vbox.pack_start(self.drawing_area) 
     self.drawing_area.show() 

     self.drawing_area.connect("expose-event", self.expose_handler) 
     self.drawing_area.show() 
     self.window.show_all() 
     self.window.connect("destroy", self.destroy) 

    def main(self): 
     glib.timeout_add_seconds(1, self.refresh_clock) 
     gtk.main() 

if __name__ == "__main__": 
    base = Base() 
    base.main() 

回答

3

您可以使用glib.timeout_add_seconds(...)設置一個計時器調用queue_draw()每秒:

import glib 

添加一個函數來刷新時鐘:

def refresh_clock(self): 
    self.drawing_area.queue_draw() 
    return True 

更新main()

def main(self): 
    glib.timeout_add_seconds(1, self.refresh_clock) 
    gtk.main() 
+0

謝謝非常acw1668,它現在工作。 – Csongor