欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Pycharm制作搞怪弹窗的实现代码

程序员文章站 2023-03-12 16:20:30
闲来无聊用python制作了一个搞怪的桌面弹窗程序,惊喜连连哦运行动图实现代码:import tkinter as tkimport tkinter.font as tkfont # 引入字体模块im...

闲来无聊用python制作了一个搞怪的桌面弹窗程序,惊喜连连哦

运行动图

Pycharm制作搞怪弹窗的实现代码

实现代码:

import tkinter as tk
import tkinter.font as tkfont # 引入字体模块
import time
import sys
import pygame
import random
import threading
import win32api
import wmi
from tkinter.messagebox import*

#播放音频
path = "mp3/暗恋.mp3"#一开始的音乐,替换成你的地址
pygame.mixer.init()
pygame.mixer.music.load(path)
pygame.mixer.music.play()

#设置样式
winwidth = 800#窗体宽度
winheight = 600#窗体高度
winx = 400#弹窗横坐标
winy = 100#弹窗纵坐标
img_x = 250#设置图片横坐标
img_y = 100#设置图片纵坐标
question_x = 250#设置问题横坐标
question_y = 60#设置问题纵坐标
button_width = 100#设置按钮宽度
button_height = 40#设置按钮高度
button_y = 520#按钮纵坐标
yes_button_x = img_x - button_width // 2#确定按钮横坐标
no_button_x = winwidth - img_x - button_width//2#否定按钮横坐标

#显示文本
global text
global title
question = "question?"
yes = "ok"
no = "wait"
title = "i need money"

#播放音频
def start_music():
  path = "mp3/殿堂.mp3"#点击ok按钮后播放的音频,这里替换成你的音频文件地址
  pygame.mixer.init()
  pygame.mixer.music.load(path)
  pygame.mixer.music.play()


#调整屏幕亮度
def screenchange() -> object:
  screen = wmi.wmi(namespace='root\wmi')
  a = screen.wmimonitorbrightnessmethods()[0]
  a.wmisetbrightness(brightness=100, timeout=500)


#调整windows音量
def changevd():
  wm_appcommand = 0x319
  appcommand_volume_max = 0x0a
  appcommand_volume_min = 0x09
  # 音量最大
  win32api.postmessage(-1, wm_appcommand, 0x30292, appcommand_volume_max * 0x10000)
  # 音量最小
  #win32api.postmessage(-1, wm_appcommand, 0x30292, appcommand_volume_min * 0x10000)


#弹窗炸弹
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(title)
  window.geometry("300x50" + "+" + str(a) + "+" + str(b))
  tk.label(window,
       text=text, # 标签的文字
       # bg='white', # 背景颜色
       font=('楷体', 17), # 字体和字体大小
       width=15, height=2 # 标签长宽
       ).pack() # 固定窗口位置
  window.mainloop()


#触发弹窗炸弹
def open_start():
  threads = []
  for i in range(50): # 需要的弹框数量
    t = threading.thread(target=dow)
    t.setdaemon(true)
    threads.append(t)
    time.sleep(0.1)
    threads[i].start()


# 新建无法直接关闭的tk类
class newtk(tk.tk):
  #重写“x”按钮
  def destroy(self):
    root = tk.tk()
    root.withdraw()
    for i in range(3):
      if i == 0:
        showinfo(title="title1", message="question1")
      if i == 1:
        showinfo(title="title2", message="question2")
      if i == 2:
        showinfo(title="title3", message="question3")
        global text
        text = "title"
        global title
        title = "text"
        open_start()

#主程序体
thread = threading.thread(target=changevd)
thread.setdaemon(true)
thread.start()
screenchange()
win = newtk()
win.title(title)
win.geometry("%sx%s+%s+%s" % (winwidth, winheight, winx, winy))#样式设置
win.resizable(0, 0)#阻止窗口大小化

photo = tk.photoimage(file="lib/没钱了.gif")#这里替换成你的图片地址
imglabel = tk.label(win, image=photo)#将图片添加至窗口
imglabel.place(x=img_x, y=img_y)#设置图片位置

question_text = tkfont.font(size=20, weight=tkfont.bold)
q = tk.label(win, text=question, font=question_text)
q.place(x=question_x, y=question_y)

#ok按钮点击事件
def click_yes():
  start_music()
  root = tk.tk()
  root.withdraw()
  for i in range(5):
    if i == 0:
      showinfo(title="title1", message="text1")
    if i == 1:
      showinfo(title="title2", message="text2")
    if i == 2:
      showinfo(title="title3", message="text3")
      global text
      text = "text"
      global title
      title = "title"
      open_start()
    if i == 3:
      print("运行退出程序")
      sys.exit(0)

yes_button = tk.button(win, text=yes, command=click_yes)
yes_button.place(x=yes_button_x, y=button_y, width=button_width, height=button_height)

no_button = tk.button(win, text=no)
no_button.place(x=no_button_x, y=button_y, width=button_width, height=button_height)

#no按钮触碰事件
def mouse_in_no_click(event):
  bx, by = random.randint(button_width, winwidth-button_width), random.randint(button_height, winheight-button_height)
  no_button.place(x=bx, y=by)

no_button.bind("<motion>", mouse_in_no_click)
win.mainloop()

学会了吗,快去试一试吧,将文本和图片替换掉又能制造更多的惊喜呦

到此这篇关于pycharm制作搞怪弹窗的实现代码的文章就介绍到这了,更多相关pycharm制作弹窗内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!