
"""
这是本人制作视频教程,用来在桌面上显示的一个字幕,
精灵模块初体验__原地踏步猫的字幕效果显示程序
,本程序应该直接用tkinter模块编写能更为简单,
这里只是演示如何重定义类的一些方法。
"""
from sprites import *
# 重定义ScrolledCanvas的初始化方法
def ScrolledCanvas_init(self,master,width=500,height=350,canvwidth=600,canvheight=500):
""" 把滚动条给去掉了"""
TK.Frame.__init__(self, master, width=width, height=height)
self._rootwindow = self.winfo_toplevel()
self.width, self.height = width, height
self.canvwidth, self.canvheight = canvwidth, canvheight
self.bg = "white"
self._canvas = TK.Canvas(master, width=width, height=height,
bg=self.bg, relief=TK.SUNKEN, borderwidth=2)
self._canvas.pack(expand=1,fill='both')
self.reset()
self._rootwindow.bind('', self.onResize)
ScrolledCanvas.__init__ = ScrolledCanvas_init
def ScrolledCanvas_reset(self, canvwidth=None, canvheight=None, bg = None):
"""重置画布"""
if canvwidth:
self.canvwidth = canvwidth
if canvheight:
self.canvheight = canvheight
if bg:
self.bg = bg
self._canvas.config(bg=bg,
scrollregion=(-self.canvwidth//2, -self.canvheight//2,
self.canvwidth//2, self.canvheight//2))
self._canvas.xview_moveto(0.5*(self.canvwidth - self.width + 30) /
self.canvwidth)
self._canvas.yview_moveto(0.5*(self.canvheight- self.height + 30) /
self.canvheight)
ScrolledCanvas.reset = ScrolledCanvas_reset
def ScrolledCanvas_adjustScrolls(self):
""" 校正画布尺寸
"""
cwidth = self._canvas.winfo_width()
cheight = self._canvas.winfo_height()
self._canvas.xview_moveto(0.5*(self.canvwidth-cwidth)/self.canvwidth)
self._canvas.yview_moveto(0.5*(self.canvheight-cheight)/self.canvheight)
ScrolledCanvas.adjustScrolls = ScrolledCanvas_adjustScrolls
s = 'Python精灵模块初体验__原地踏步猫'
screen = Screen()
screen.bgcolor('red')
screen.titlebar(False)
root = screen._root # 窗口对象
root.wm_attributes('-alpha',0.7) # 设置窗口为全透明(0到1.0)
screen.setup(800,100)
screen.tracer(0,0)
# 下面的代码让窗口可以拖动.
oldx = 0
oldy = 0
def startmove(event):
global oldx,oldy
oldx = event.x
oldy = event.y
def stopmove(event):
global oldx,oldy
oldx = 0
oldy = 0
def movewindow(event):
global oldx,oldy
dx = event.x - oldx
dy = event.y - oldy
root.move(dx,dy)
screen.cv.bind("", startmove)
screen.cv.bind("", stopmove)
screen.cv.bind("",movewindow)
# 下面的代码按方向箭头则窗口能上下左右移动
screen.onkeypress(lambda:root.move(10),'Right')
screen.onkeypress(lambda:root.move(-10),'Left')
screen.onkeypress(lambda:root.move(0,-10),'Up')
screen.onkeypress(lambda:root.move(0,10),'Down')
screen.listen()
# 下面的TK是就是tkinter
popup = TK.Menu(screen._root, tearoff=0)
popup.add_command(label="关于本程序",command=lambda:showinfo('关于','本程序由李兴球开发\n\nQQ:406273900\n\nwww.lixingqiu.com'))
popup.add_command(label="打开主页",command=lambda:os.system('explorer https://www.lixingqiu.com'))
popup.add_command(label="退出本程序",command=lambda:screen.bye())
def do_popup(event):
"""显示弹出菜单"""
try:
popup.tk_popup(event.x_root, event.y_root + 10, 0)
finally:
# 确保释放按键
popup.grab_release()
screen.cv.bind("", do_popup) # 画布绑定鼠标右键
for x in range(400):
root.move(0,-1)
root.update()
time.sleep(0.01)
# 单击鼠标左键开始
#m1 = Mouse() # 鼠标左键
#while not m1.down():screen.update()
ft_context = ('微软雅黑',24,'normal')
t = Sprite(visible=False,pos=(-600,-15))
t.color('white')
clock = Clock()
t.write(s,align='center',font=ft_context)
worditem = t.items[-1] # 字的编号
wordbbox = screen.cv.bbox(worditem) # 字的绑定盒子
wordwidth = wordbbox[-1] - wordbbox[0] # 字的宽度
def scrolledtext():
t.clear()
t.write(s,align='center',font=ft_context)
t.wait()
t.fd(2)
if t.xcor() - wordwidth/2 >300:t.setx(-200-wordwidth/2)
clock.tick(60)
if t.xcor()==0:t.wait(1)
screen.ontimer(scrolledtext)
scrolledtext()
screen.mainloop()