将 "数据与人" 设为 "星标⭐"
第一时间收到文章更新
通过 Pyqt5 模块构建透明窗口,并设置聊天窗等功能,设置右键功能,使其功能启动“浪漫程序“;
开始宠物系统再通过多线程启动其他程序,包含弹窗程序,海龟画图程序等;
生成爱心照片墙:通过 2.py 程序生成爱心照片墙,并缩放至1000×806;
海龟画图:运行 1.py 程序实现海龟画图,绘制玫瑰花和爱心;
1、照片墙生成程序
random.shuffle(imgs)
img_h = img_w = 250
rows = len(map)
columns = len(map[0])
figure = Image.new("RGB", (img_w * columns, img_h * rows), "white")
count = 0
for i in range(len(map)):
for j in range(len(map[i])):
if map[i][j] == 1:
continue
else:
try:
image = Image.open(os.path.join(img_dir, imgs[count]))
except:
continue
image = image.resize((img_w, img_h))
figure.paste(image, (img_w * j, img_h * i))
count += 1
2、启动宠物程序
1、Pyqt5 基本流程
def __init__(self):
super(DemoWin, self).__init__()
self.initUI()
# 初始化,不规则窗口
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.SubWindow)
self.setAutoFillBackground(False)
self.setAttribute(Qt.WA_TranslucentBackground, True)
self.repaint()
# 是否跟随鼠标
self.is_follow_mouse = False
self.move(1650,20)
with open("data.txt", "r") as f:
text = f.read()
self.sentence = text.split("\n")
QtCore.Qt.NoButton - 0 - 没有按下鼠标键
QtCore.Qt.LeftButton -1 -按下鼠标左键
QtCore.Qt.RightButton -2 -按下鼠标右键
QtCore.Qt.Mion 或 QtCore.Qt.MiddleButton -4 -按下鼠标中键
# 当按右键的时候,这个event会被触发
def contextMenuEvent(self, event):
menu = QMenu(self)
video = menu.addAction("浪漫程序")
quitAction = menu.addAction("退出")
action = menu.exec_(self.mapToGlobal(event.pos()))
if action == quitAction:
qApp.quit()
if action == video:
import threading
def t2():
os.system("python 1.py")
t22=threading.Thread(target=t2)
t22.start()
def quit(self):
self.close()
sys.exit()
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setWindowIcon(QIcon("1.jpg"))
# 创建一个主窗口
mainWin = DemoWin()
# 显示
mainWin.show()
# 主循环
sys.exit(app.exec_())
3、turtle 画图
wn = turtle.Screen()
wn.screensize()
wn.setup(width=1.0, height=1.0)
wn.register_shape('1.gif')
rocket = turtle.Turtle()
rocket.shape('1.gif')
color('red', 'pink')
getscreen().tracer(1, 0)
heart(100, 0, 0.7)
go_to(80, 70)
write("小明", font=("楷体", 18, "normal"))
setheading(0)
heart(-80, -100, 1)
go_to(-110, 15)
write("小红", font=("宋体", 20, "normal"))
go_to(40, -80)
write("三生三世皆是你!", move=True, align="left", font=("arial", 22, "italic"))
time.sleep(2)
wn.clear()
4、弹窗程序
def dow():
window = tk.Tk()
width = window.winfo_screenwidth()
height = window.winfo_screenheight()
a = random.randrange(0, width)
b = random.randrange(0, height)
window.title('小明表白小红') # 弹窗的名字
window.geometry("200x50" + "+" + str(a) + "+" + str(b)) # 弹窗大小 tk.Label(window,
text='小明表白小红!', # 标签的文字
bg='Red', # 背景颜色
font=('楷体', 17), # 字体和字体大小
width=15, height=2 # 标签长宽
).pack() # 固定窗口位置
window.mainloop()
threads = []
for i in range(100):
t = threading.Thread(target=dow)
threads.append(t)
time.sleep(0.1)
threads[i].start()
完整代码详见:https://gitcode.net/qq_42279468/gaobai/-/tree/master