镇江专业建网站,福州网站设计定制公司,网站模板破解下载,网线制作的注意事项Canvas对象生成之后#xff0c;有时会希望调整对象的位置。例如前面文章中提到的时钟小程序#xff0c;我们稍加改造可以另外实现一个指针式时钟#xff1a;在这个小程序中增加的功能就是根据具体时间计算每个指针的坐标信息#xff0c;这部分功能在时钟类Clock中实现。这个…Canvas对象生成之后有时会希望调整对象的位置。例如前面文章中提到的时钟小程序我们稍加改造可以另外实现一个指针式时钟在这个小程序中增加的功能就是根据具体时间计算每个指针的坐标信息这部分功能在时钟类Clock中实现。这个Clock类修改自前一篇文章中的DitialClock类class Clock:def __init__(self, canvas, width, height):self.canvas canvasself.width widthself.height heightself.digital Trueself.type None# create font for date.ftDate Font(familyTimes, size32)self.canvas.create_text(width / 2, height / 4,text,fontftDate,tagdate)# create font for time.self.ftTime Font(familyTimes, size64)self.set_type(Digital)到14行为止的内容都和DitgitalClock相同第15行调用set_type方法来选择时钟的类型def set_type(self, type):if typeDigital:self.canvas.create_text(self.width / 2, self.height / 2,text,fontself.ftTime,tagtime)self.canvas.delete(hour)self.canvas.delete(minute)self.canvas.delete(second)self.canvas.delete(center)else:self.canvas.delete(time)self.canvas.create_line(self.width / 2, self.height / 2,self.width / 2, self.height / 2,width15,fillred,arrowLAST,arrowshape(self.width / 20, self.width / 10, self.width / 40),taghour)self.canvas.create_line(self.width / 2, self.height / 2,self.width / 2, self.height / 2,width10,fillgreen,capstyleROUND,tagminute)self.canvas.create_line(self.width / 2, self.height / 2,self.width / 2, self.height / 2,width3,fillblue,capstyleROUND,tagsecond)center_r 10self.canvas.create_oval(self.width / 2 - center_r,self.height / 2 - center_r,self.width / 2 center_r,self.height / 2 center_r,fillwhite,tagcenter)self.type typeself.update()代码的内容虽长内容却很简单构建需要的对象消除不需要的对象。更新时钟的内容则是根据类型对不同的对象进行更新def update(self): now time.localtime() time_str time.strftime(%Y.%m.%d %a %p, now) self.canvas.itemconfigure(date, texttime_str) if typeDigital: time_str time.strftime(%I:%M:%S, now) self.canvas.itemconfigure(time, texttime_str) else: self.draw_hour(now.tm_hour) self.draw_minute(now.tm_min) self.draw_second(now.tm_sec)描画指针的部分是指针式时钟特有的部分其内容是根据小时分秒分别计算每个指针的坐标并更新到相应的对象。在Canvas中可以使用coords方法为对象设置新坐标。Tkinter中更新坐标信息之后并不需要另外调用一个画面更新之类的方法更新结果会直接反映到画面上。def update(self):now time.localtime()time_str time.strftime(%Y.%m.%d %a %p, now)self.canvas.itemconfigure(date, texttime_str)if self.typeDigital:time_str time.strftime(%I:%M:%S, now)self.canvas.itemconfigure(time, texttime_str)else:self.draw_hour(now.tm_hour)self.draw_minute(now.tm_min)self.draw_second(now.tm_sec)def draw_second(self, second):self.__draw_hand(second, self.width * 0.4, second, 60)def draw_minute(self, minute):self.__draw_hand(minute, self.width * 0.3, minute, 60)def draw_hour(self, hour):self.__draw_hand(hour, self.width * 0.25, hour % 12, 12)def __draw_hand(self, hand, radius, value, system):radians value / system * 2 * math.pi - math.pi / 2self.canvas.coords(hand,self.width / 2, self.height / 2,self.width / 2 radius * math.cos(radians),self.height / 2 radius * math.sin(radians))接下来是主程序首先是构建主窗口。和之前的代码稍有不同代码禁止了主窗口的大小调整功能并为之设置了标题。# create the main windowroot Tk()root.resizable(False, False)root.title(Tkinter Clock V1.0)增加一个OptionMenu控件用于切换数字式时钟和指针式时钟。clock_type StringVar()clock_type.set(Digital)enable_menu OptionMenu(root, clock_type, Digital, Analog )enable_menu.grid(row 0, column 0, stickyW)构建Canvas和时钟对象。# create canvascanvas Canvas(root, height 400, width 400, reliefSUNKEN)canvas.grid(row1, column0)clock Clock(canvas, 400, 400)监视变量的变化并进行时钟类型切换def var_changed(*args):clock.set_type(clock_type.get())# set variable observer.clock_type.trace_variable(w, var_changed)构建并启动定时器timer Timer(root, 1000, clock.update)timer.start()启动主窗口并在mainloop结束后关闭定时器root.mainloop()timer.stop()完整代码可以从以下地址下载https://github.com/xueweiguo/TkinterPrimer/blob/master/Sample/25%20AnalogClock.py觉得本文有帮助请分享给更多人。关注【面向对象思考】轻松学习每一天面向对象设计面向对象编程面向对象思考