設計乙個gui類來顯示乙個時鐘,假設要在乙個畫布內顯示它而且能夠在其他程式中。需要定義乙個時鐘類來實現時鐘的重用。進一步說,為了在圖形上顯示這個時鐘,需要將它定義為乙個widget小構件。因此最好的選擇是定義時鐘類擴充套件canvas類,使時鐘物件能夠像canvas物件一樣使用。
定義時鐘類stillclock為canvas的擴充套件,**如下:
# coding: utf-8
from tkinter import * #import all definitions from tkinter
import math
from datetime import datetime
class
stillclock
(canvas):
def__int__
(self, container):
canvas.__init__(container)
self.setcurrenttime()
defgethour
(self):
return self.__hour
defsethour
(self, hour):
self.__hour = hour
self.delete('clock')
self.drawclock()
defgetminute
(self):
return self.__minute
defsetmnite
(self, minute):
self.__minute = minute
self.delete("clock")
self.drawclock()
defgetsecond
(self):
return self.__second
defsetsecond
(self, second):
self.__second = second
self.delete("clock")
self.drawclock()
defsetcurrenttime
(self):
d = datetime.now()
self.__hour = d.hour
self.__minute = d.minute
self.__second = d.second
self.delete("clock")
self.drawclock()
defdrawclock
(self):
width = float(self["width"])
height = float(self["height"])
radius = min(min, height) / 2.4
secondhandlength = radius * 0.8
minutehandlength = radius * 0.65
hourhandlength = radius * 0.5
self.create_oval(width / 2 - radius, height / 2 - radius,
width / 2 + radius, height / 2 + radius, tags = "clock")
self.create_text(width / 2 - radius + 5, height / 2,
text = "9", tags = "clock")
self.create_text(width / 2 + radius - 5, height / 2,
text = "3", tags = "clock")
self.create_text(width / 2, height / 2 - radius + 5,
text = "12", tags = "clock")
self.create_text(width / 2, height / 2 + radius - 5,
text = "6", tags = "clock")
xcenter = width / 2
ycenter = height / 2
second = self.__second
xsecond = xcenter + secondhandlength \
* math.sin(second * (2 * math.pi / 60))
ysecond = ycenter - secondhandlength \
* math.sin(second * (2 * math.pi / 60))
self.create_line(xcenter, ycenter, xsecond, ysecond,
fill = "red", tags = "clock")
minute = self.__minute
xminute = xcenter + \
minutehandlength * math.sin(minute * (2 * math.pi / 60))
yminute = ycenter - \
minutehandlength * math.cos(minute * (2 * math.pi / 60))
self.create_line(xcenter, ycenter, xminute, yminute,
fill = "blue", tags = "clock")
hour = self.__hour % 12
xhour = xcenter + hourhandlength * \
math.sin((hour + minute / 60) * (2 * math.pi / 12))
yhour = ycenter - hourhandlength * \
math.cos((hour + minute / 60) * (2 * math.pi / 12))
self.create_line(xcenter, ycenter, xhour, yhour,
fill = "green", tags = "clock")
timestr = str(hour) + ":" +str(minute) + ":" + str(second)
self.create_text(width / 2, height / 2 + radius + 10,
text = timestr, tags = "clock")
stillclock類擴充套件自canvas小構件,因此可以像使用canvas一樣使用stillclock.
—摘自《python 語言設計》
可重用類設計 多語言功能開發
背景 公司的電子商務網提供多語言的支援。開發語言為c net 最初每個 有一組本地化檔案。分別用resx檔案儲存,各式例如 text.en us.resx.然後第乙個類,實現讀取.resx檔案到快取中。再次使用時直接從快取中查詢資料。然後每到乙個新的專案,直接複製此類檔案,然後稍作修改,就又能在新專...
可重用設計 使用符號常量和parameter
可重用設計 使用符號常量和parameter 好的設計習慣會在hdl 中避免使用 魔鬼數字 而是盡量採用符號常量。verilog hdl中,使用關鍵字localparam宣告符號常量 4位全加器的verilog hdl描述 module adder carry hard lit input wire...
將menu設計為可重用的子系統
這篇部落格對應線上課程第七次實驗。網易雲課堂 實驗樓 庫 需要新增兩個函式menuconfig,executemenu用於新增命令和執行menu程式 另外需要handleargv處理命令字串集,再傳給執行命令的具體函式。編寫makefile可以實現自動化編譯,編寫完成後,使用make命令,gcc可以...