python canvas画移动物体_Python GUI编程入门(25)-移动Canvas对象
Canvas對象生成之后,有時(shí)會(huì)希望調(diào)整對象的位置。例如前面文章中提到的時(shí)鐘小程序,我們稍加改造可以另外實(shí)現(xiàn)一個(gè)指針式時(shí)鐘:
在這個(gè)小程序中增加的功能就是根據(jù)具體時(shí)間計(jì)算每個(gè)指針的坐標(biāo)信息,這部分功能在時(shí)鐘類Clock中實(shí)現(xiàn)。這個(gè)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行為止的內(nèi)容都和DitgitalClock相同,第15行調(diào)用set_type方法來選擇時(shí)鐘的類型:
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()
代碼的內(nèi)容雖長,內(nèi)容卻很簡單:構(gòu)建需要的對象,消除不需要的對象。更新時(shí)鐘的內(nèi)容則是根據(jù)類型對不同的對象進(jìn)行更新:
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)
描畫指針的部分是指針式時(shí)鐘特有的部分,其內(nèi)容是根據(jù)小時(shí),分,秒分別計(jì)算每個(gè)指針的坐標(biāo)并更新到相應(yīng)的對象。在Canvas中可以使用coords方法為對象設(shè)置新坐標(biāo)。Tkinter中更新坐標(biāo)信息之后并不需要另外調(diào)用一個(gè)畫面更新之類的方法,更新結(jié)果會(huì)直接反映到畫面上。
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))
接下來是主程序,首先是構(gòu)建主窗口。和之前的代碼稍有不同,代碼禁止了主窗口的大小調(diào)整功能并為之設(shè)置了標(biāo)題。
# create the main window
root = Tk()
root.resizable(False, False)
root.title('Tkinter Clock V1.0')
增加一個(gè)OptionMenu控件用于切換數(shù)字式時(shí)鐘和指針式時(shí)鐘。
clock_type = StringVar()
clock_type.set('Digital')
enable_menu = OptionMenu(root, clock_type, 'Digital', 'Analog ')
enable_menu.grid(row = 0, column = 0, sticky=W)
構(gòu)建Canvas和時(shí)鐘對象。
# create canvas
canvas = Canvas(root, height= 400, width= 400, relief=SUNKEN)
canvas.grid(row=1, column=0)
clock = Clock(canvas, 400, 400)
監(jiān)視變量的變化并進(jìn)行時(shí)鐘類型切換:
def var_changed(*args):
clock.set_type(clock_type.get())
# set variable observer.
clock_type.trace_variable('w', var_changed)
構(gòu)建并啟動(dòng)定時(shí)器:
timer = Timer(root, 1000, clock.update)
timer.start()
啟動(dòng)主窗口,并在mainloop結(jié)束后關(guān)閉定時(shí)器:
root.mainloop()
timer.stop()
完整代碼可以從以下地址下載:
https://github.com/xueweiguo/TkinterPrimer/blob/master/Sample/25%20AnalogClock.py
覺得本文有幫助?請分享給更多人。
關(guān)注【面向?qū)ο笏伎肌?#xff0c;輕松學(xué)習(xí)每一天!
面向?qū)ο笤O(shè)計(jì),面向?qū)ο缶幊?#xff0c;面向?qū)ο笏伎?#xff01;
總結(jié)
以上是生活随笔為你收集整理的python canvas画移动物体_Python GUI编程入门(25)-移动Canvas对象的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 延迟实例化_延迟初始化Spri
- 下一篇: 天猫精灵方糖拆解报告和芯片详解