python canvas画移动物体_Python GUI编程入门(25)-移动Canvas对象
Canvas對象生成之后,有時會希望調整對象的位置。例如前面文章中提到的時鐘小程序,我們稍加改造可以另外實現一個指針式時鐘:
在這個小程序中增加的功能就是根據具體時間計算每個指針的坐標信息,這部分功能在時鐘類Clock中實現。這個Clock類修改自前一篇文章中的DitialClock類:
class Clock:
def __init__(self, canvas, width, height):
self.canvas = canvas
self.width = width
self.height = height
self.digital = True
self.type = None
# create font for date.
ftDate = Font(family='Times', size=32)
self.canvas.create_text(width / 2, height / 4,
text='',
font=ftDate,
tag='date')
# create font for time.
self.ftTime = Font(family='Times', size=64)
self.set_type('Digital')
到14行為止的內容都和DitgitalClock相同,第15行調用set_type方法來選擇時鐘的類型:
def set_type(self, type):
if type=='Digital':
self.canvas.create_text(self.width / 2, self.height / 2,
text='',
font=self.ftTime,
tag='time')
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,
width=15,
fill='red',
arrow=LAST,
arrowshape=(self.width / 20, self.width / 10, self.width / 40),
tag='hour')
self.canvas.create_line(self.width / 2, self.height / 2,
self.width / 2, self.height / 2,
width=10,
fill='green',
capstyle=ROUND,
tag='minute')
self.canvas.create_line(self.width / 2, self.height / 2,
self.width / 2, self.height / 2,
width=3,
fill='blue',
capstyle=ROUND,
tag='second')
center_r = 10
self.canvas.create_oval(self.width / 2 - center_r,
self.height / 2 - center_r,
self.width / 2 + center_r,
self.height / 2 + center_r,
fill='white',
tag='center')
self.type = type
self.update()
代碼的內容雖長,內容卻很簡單:構建需要的對象,消除不需要的對象。更新時鐘的內容則是根據類型對不同的對象進行更新:
def update(self): now = time.localtime() time_str = time.strftime('%Y.%m.%d %a %p', now) self.canvas.itemconfigure('date', text=time_str) if type=='Digital': time_str = time.strftime('%I:%M:%S', now) self.canvas.itemconfigure('time', text=time_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', text=time_str)
if self.type=='Digital':
time_str = time.strftime('%I:%M:%S', now)
self.canvas.itemconfigure('time', text=time_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 / 2
self.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 window
root = 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, sticky=W)
構建Canvas和時鐘對象。
# create canvas
canvas = Canvas(root, height= 400, width= 400, relief=SUNKEN)
canvas.grid(row=1, column=0)
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
覺得本文有幫助?請分享給更多人。
關注【面向對象思考】,輕松學習每一天!
面向對象設計,面向對象編程,面向對象思考!
總結
以上是生活随笔為你收集整理的python canvas画移动物体_Python GUI编程入门(25)-移动Canvas对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 延迟实例化_延迟初始化Spri
- 下一篇: python合并单元格 索引_pytho