Python恶搞代码

在这里插入图片描述

文章目录

  • 系列文章
  • 前言
    • Tkinter界面设计
    • Threading多线程
    • 恶搞代码
    • 完整代码
  • 尾声

系列文章

序号 文章目录 直达链接
表白系列
1 无法拒绝的表白界面 https://want595.blog.csdn.net/article/details/134744894
2 满屏飘字表白代码 https://want595.blog.csdn.net/article/details/135037388
3 无限弹窗表白代码 https://want595.blog.csdn.net/article/details/134744711
4 李峋同款可写字版跳动的爱心 https://want595.blog.csdn.net/article/details/134744191
5 流星雨 https://want595.blog.csdn.net/article/details/134747408
6 漂浮爱心 https://want595.blog.csdn.net/article/details/134744929
7 爱心光波 https://want595.blog.csdn.net/article/details/134747365
8 玫瑰花 https://want595.blog.csdn.net/article/details/134747447
节日系列
1 新春/跨年 烟花秀(2022) https://want595.blog.csdn.net/article/details/128727394
烟花秀(2023) https://want595.blog.csdn.net/article/details/135042880
粒子烟花 https://want595.blog.csdn.net/article/details/136029420
2 圣诞节 圣诞礼物 https://want595.blog.csdn.net/article/details/135336583
圣诞树(2022) https://want595.blog.csdn.net/article/details/128428985
绿色圣诞树(2023) https://want595.blog.csdn.net/article/details/135048607
粉色圣诞树(2023) https://want595.blog.csdn.net/article/details/135043042
3 冬至 大雪纷飞 https://want595.blog.csdn.net/article/details/128806017
4 生日 生日蛋糕 https://want595.blog.csdn.net/article/details/128739755
5 儿童节 五彩气球 https://want595.blog.csdn.net/article/details/128741043
6 国庆节 国庆祝福 https://want595.blog.csdn.net/article/details/128740923
7 万圣节 万圣礼物 https://want595.blog.csdn.net/article/details/128734395
8 愚人节 愚人代码 https://want595.blog.csdn.net/article/details/128696990
9 中秋节 浪漫星空 https://want595.blog.csdn.net/article/details/128737284
10 植树节 樱花树 https://want595.blog.csdn.net/article/details/128700178
动漫系列
1 名侦探柯南系列 柯南 https://want595.blog.csdn.net/article/details/134777613
2 喜羊羊与灰太狼系列 喜羊羊 https://want595.blog.csdn.net/article/details/134778583
懒羊羊 https://want595.blog.csdn.net/article/details/134847642
灰太狼 https://want595.blog.csdn.net/article/details/135335303
小灰灰 https://want595.blog.csdn.net/article/details/135335445
小香香 https://want595.blog.csdn.net/article/details/135056783
3 海绵宝宝系列 海绵宝宝 https://want595.blog.csdn.net/article/details/134847364
4 哆啦A梦系列 哆啦A梦 https://want595.blog.csdn.net/article/details/135037884
5 HelloKitty系列 hellokitty https://want595.blog.csdn.net/article/details/135337732
6 Tom&Jerry系列 Tom&Jerry https://want595.blog.csdn.net/article/details/135337775
7 草莓熊系列 草莓熊 https://want595.blog.csdn.net/article/details/135337832
8 皮卡丘系列 迷你皮卡丘 https://want595.blog.csdn.net/article/details/135337911
高级皮卡丘 https://want595.blog.csdn.net/article/details/135337937
豪华皮卡丘 https://want595.blog.csdn.net/article/details/135337947
炫酷系列
1   一闪一闪亮星星系列 张万森下雪了 https://want595.blog.csdn.net/article/details/135336915
一闪一闪亮星星 https://want595.blog.csdn.net/article/details/135337049
2 代码雨 https://want595.blog.csdn.net/article/details/135054341
3 七彩花朵 https://want595.blog.csdn.net/article/details/135056670
4 3D星空 https://want595.blog.csdn.net/article/details/135056516
5 金榜题名 https://want595.blog.csdn.net/article/details/135056150
6 满天星 https://want595.blog.csdn.net/article/details/135056305
……

前言

快来领取python无限弹窗恶搞代码吧!每天写一些有趣的小程序,带你成为一个有趣的程序员!

Tkinter界面设计

1. 创建一个简单的界面

Tkinter 是 Python 标准库中的一个 GUI(图形用户界面)模块,它可以让你创建窗口、标签、按钮、菜单等等交互式的界面。以下是 Tkinter 中一些简单的函数使用方法。

  1. 导入 Tkinter 包:
import tkinter
  1. 创建主窗口:
root = tkinter.Tk()
  1. 创建标签:
label = tkinter.Label(root, text="Hello, World!")
  1. 显示标签:
label.pack()
  1. 进入主循环:
root.mainloop()

完整的程序:

import tkinter

root = tkinter.Tk()
label = tkinter.Label(root, text="Hello, World!")
label.pack()
root.mainloop()

这个程序会创建一个带有 “Hello, World!” 标签的窗口,并且会一直保持在屏幕上直到退出程序。

2. 简单的控件

在 Tkinter 中,有许多控件可用来创建图形用户界面。下面是一些简单的控件及其用法:

  1. Label (标签)

用于显示文本或图像。

import tkinter

root = tkinter.Tk()

label = tkinter.Label(root, text = "Hello World!")
label.pack()

root.mainloop()
  1. Button (按钮)

用于执行操作或触发事件。

import tkinter

root = tkinter.Tk()

def buttonClicked():
    print("Button clicked")

button = tkinter.Button(root, text = "Click me", command = buttonClicked)
button.pack()

root.mainloop()
  1. Entry (输入框)

用于获取用户输入的文本。

import tkinter

root = tkinter.Tk()

entry = tkinter.Entry(root)
entry.pack()

def buttonClicked():
    print("The text entered is:", entry.get())

button = tkinter.Button(root, text = "Submit", command = buttonClicked)
button.pack()

root.mainloop()

以上控件都是 Tkinter 中的基本控件,掌握了这些,就可以开始创建简单的GUI程序了。

Threading多线程

在 Python 中,可以使用 threading 模块来创建和管理线程。线程是程序执行流的最小单元,不同于进程,所有线程共享同一份数据。下面是一些简单的 threading 使用方法和函数。

  1. 导入 threading 模块
import threading
  1. 创建线程

可以使用 Thread 类创建一个线程。需要给类的构造函数传递一个可调用的函数作为参数,这个函数将会在线程中运行。

def myThread():
    print("Thread is running")

thread = threading.Thread(target=myThread)
thread.start()
  1. 线程间通信

可以使用队列(Queue)和共享内存(Value 和 Array)等机制在线程间传递数据。

使用 Queue:

import threading
import queue

queue = queue.Queue()

def myThread(queue, message):
    queue.put(message)

thread = threading.Thread(target=myThread, args=(queue, 'Hello, World'))
thread.start()

message = queue.get()
print(message)

使用 Value:

import threading

value = threading.Value('i', 0)

def myThread(value):
    value.value += 1

thread = threading.Thread(target=myThread, args=(value,))
thread.start()

print(value.value)

以上是一些线程使用方法和函数的示例。需要注意的是,多线程程序的正确性可能会受到许多因素的影响,比如数据竞争、死锁、饥饿等等,需要仔细考虑和设计线程间的交互机制。

恶搞代码

在简单了解了Tkinter界面设计以及Threading多线程后,我们就可以写一个恶搞好友的程序啦!

1. 恶作剧界面

以下程序实现了一个简单的恶搞界面

def Death():
    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='black',font=("Comic Sans MS",15),width=30,height=5).pack()
    root.mainloop()

2. 恶搞界面的数量

建议for循环中的层数设置适当,避免程序复杂度过大导致系统崩溃(以下代码将for循环设置了十层,会产生10个小窗体)

def Start():
    for i in range(10):
        t=td.Thread(target=Death)
        ti.sleep(0.1)
        t.start()

完整代码

https://want595.blog.csdn.net/article/details/128696990

尾声

感谢支持吖!

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/a65ac80c92.html