前言
我闲着无聊写了个Python表白程序分享给大家,程序会在屏幕上显示一个大红心和多个随机位置的“我喜欢你”窗口。这是通过Tkinter库创建GUI,random库生成随机坐标,threading库实现多线程,营造出满屏表白的浪漫效果。代码适合初学者理解,涉及到多线程和随机数的应用
一、Tkinter是什么?
- Tkinter是Python标准库中的一个GUI(Graphical User Interface,图形用户界 面)工具包,其目的是为Python开发者提供快捷创建GUI应用程序的方式。
- Tkinter基于Tcl/Tk图形库,允许我们使用Python代码来创建和管理窗口、标签、按钮、复选框、文本框、列表框、滚动条、画布、菜单等多种控件和组件。 Tkinter对多数平台都有良好的支持,而无需安装额外的软件或库
二、完整代码
1.引用tkinter库
代码如下
import tkinter as tk
import random as ra
import threading as td
import time as ti
2.无限弹窗
代码如下(示例):
def Love():
root = tk.Tk()
width = 200
height = 50
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
x = ra.randint(0, screenwidth)
y = ra.randint(0, screenheight)
root.title("❤")
root.geometry("%dx%d+%d+%d" % (width, height, x, y))
tk.Label(root, text='我喜欢你', fg='white', bg='pink', font=("Comic Sans MS", 15), width=30, height=5).pack()
root.mainloop()
3.爱心页面
def Heart():
root = tk.Tk()
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
width = 600
height = 400
x = (screenwidth-width)//2
y = (screenheight-height)//2
root.title("❤")
root.geometry("%dx%d+%d+%d" % (screenwidth, screenheight, 0, 0))
tk.Label(root, text='❤', fg='pink', bg='white', font=("Comic Sans MS", 500), width=300, height=20).pack()
root.mainloop()