一、前言
我們已經了解到tkinter可以制作愛心,彈幕,為了能讓他看起來更加的充滿心意,于是,我們決定將他制作為愛心雨。讓它看起來更加的特別,達到特別的需求。
(一)Python-tkinter桌面應用開發(fā)(多彈窗)
(二)Python-tkinter桌面應用(桌面上的彈幕)
二、需求
我們要讓我們的愛心移動起來,達到下雨的需求。
三、思路
已經有需求了,我們來整理一下思路:
- 實現窗口,窗口愛心
- 讓窗口一直移動起來
- 控制彈幕出現的位置
- 控制彈幕的數量
四、代碼實現
這次,我們直接來實現愛心雨,讓大家感受一下愛心雨的魅力。
1、從上往下
?def move_down(self):
????"""
????控制移動方向,向下移動
????:return:
????"""
????self.x = str(int(self.x) + 5)
????self.tk.geometry(str(self.ww) + "x" + str(self.hh) + "+" + str(self.y) + "+" + str(self.x))
????if int(self.x) >= self.h:
????????self.x = -self.hh
????????self.tk.after(10, self.move_down)
????else:
????????self.tk.after(10, self.move_down)
2、從下往上
def move_up(self):
????"""
????控制移動方向,向上移動
????:return:
????"""
????self.x = str(int(self.x) - 5)
????self.tk.geometry(str(self.ww) + "x" + str(self.hh) + "+" + + str(self.y) + "+" + str(self.x))
????if int(self.x) <= -self.hh:
????????self.x = self.h
????????self.tk.after(10, self.move_up)
????else:
????????self.tk.after(10, self.move_up)
3、斜向左下角
def move_left_down(self):
????"""
????控制移動方向,向左下方移動,如果超過屏幕則開始循環(huán)移動
????:return:
????"""
????self.y = str(int(self.y) - 5)
????self.x = str(int(self.x) + 5)
????self.tk.geometry(str(self.ww) + "x" + str(self.hh) + "+" + str(self.y) + "+" + str(self.x))
????if int(self.y) <= -self.ww:
????????self.y = str(random.randint(0, self.w + self.ww))
????????self.x = -self.hh
????????self.tk.after(10, self.move_left_down)
????else:
????????self.tk.after(10, self.move_left_down)
4、斜向右下角
def move_right_down(self):
????"""
????控制移動方向,向右下方移動,如果超過屏幕則開始循環(huán)移動
????:return:
????"""
????self.y = str(int(self.y) + 5)
????self.x = str(int(self.x) + 5)
????self.tk.geometry(str(self.ww) + "x" + str(self.hh) + "+" + str(self.y) + "+" + str(self.x))
????if int(self.y) >= self.w + self.ww or int(self.x) >= self.h + self.hh:
????????self.y = str(random.randint(-self.ww, self.w))
????????self.x = -self.hh
????????self.tk.after(10, self.move_right_down)
????else:
????????self.tk.after(10, self.move_right_down)
文章內容
本文主要是關于講述了tkinter, ?after的用法,延伸的移動窗口的方式。操作簡單,
涉及的函數圖像有 f(x)=a,a為常數
以及f(x)=kx+b ?,k,b為常數。文章來源:http://www.zghlxwxcb.cn/news/detail-419216.html
公眾號回復 “愛心彈窗” 獲取源代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-419216.html
到了這里,關于(三)Python-tkinter桌面應用(愛心雨)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!