gpt4 book ai didi

python - 如何在 GTK 中更新绘图区域

转载 作者:行者123 更新时间:2023-11-28 21:45:22 27 4
gpt4 key购买 nike

我正在尝试在 python 2 中使用 GTK 制作一个时钟。我目前拥有的版本只是一个基本版本,它会复杂得多,但是我需要先刷新它。希望最终它会成为一个屏幕保护程序。

我试图了解 queue_draw() 的工作原理,但我无法弄明白。我是 GTK 的新手,没有使用类的经验(尽管我正在尝试)。我猜这两个导致了我的问题。有人可以帮我解决这部分吗:D我知道有关于这个问题的帖子,但我仍然不知道如何让它工作:(谢谢

我正在使用 Ubuntu 14.04,python 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()

最佳答案

您可以使用 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()

关于python - 如何在 GTK 中更新绘图区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39553371/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com