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

时间:2022-02-17 21:02:10

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

运行动图

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

实现代码:

  1. import tkinter as tk
  2. import tkinter.font as tkFont # 引入字体模块
  3. import time
  4. import sys
  5. import pygame
  6. import random
  7. import threading
  8. import win32api
  9. import wmi
  10. from tkinter.messagebox import*
  11.  
  12. #播放音频
  13. path = "mp3/暗恋.mp3"#一开始的音乐,替换成你的地址
  14. pygame.mixer.init()
  15. pygame.mixer.music.load(path)
  16. pygame.mixer.music.play()
  17.  
  18. #设置样式
  19. WINWIDTH = 800#窗体宽度
  20. WINHEIGHT = 600#窗体高度
  21. WINX = 400#弹窗横坐标
  22. WINY = 100#弹窗纵坐标
  23. img_x = 250#设置图片横坐标
  24. img_y = 100#设置图片纵坐标
  25. question_x = 250#设置问题横坐标
  26. question_y = 60#设置问题纵坐标
  27. button_width = 100#设置按钮宽度
  28. button_height = 40#设置按钮高度
  29. button_y = 520#按钮纵坐标
  30. yes_button_x = img_x - button_width // 2#确定按钮横坐标
  31. no_button_x = WINWIDTH - img_x - button_width//2#否定按钮横坐标
  32.  
  33. #显示文本
  34. global text
  35. global title
  36. question = "question?"
  37. yes = "OK"
  38. no = "Wait"
  39. title = "I need money"
  40.  
  41. #播放音频
  42. def Start_music():
  43. path = "mp3/殿堂.mp3"#点击ok按钮后播放的音频,这里替换成你的音频文件地址
  44. pygame.mixer.init()
  45. pygame.mixer.music.load(path)
  46. pygame.mixer.music.play()
  47.  
  48. #调整屏幕亮度
  49. def ScreenChange() -> object:
  50. SCREEN = wmi.WMI(namespace='root\WMI')
  51. a = SCREEN.WmiMonitorBrightnessMethods()[0]
  52. a.WmiSetBrightness(Brightness=100, Timeout=500)
  53.  
  54. #调整Windows音量
  55. def changeVd():
  56. WM_APPCOMMAND = 0x319
  57. APPCOMMAND_VOLUME_MAX = 0x0a
  58. APPCOMMAND_VOLUME_MIN = 0x09
  59. # 音量最大
  60. win32api.PostMessage(-1, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_MAX * 0x10000)
  61. # 音量最小
  62. #win32api.PostMessage(-1, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_MIN * 0x10000)
  63.  
  64. #弹窗炸弹
  65. def dow():
  66. window = tk.Tk()
  67. width = window.winfo_screenwidth()
  68. height = window.winfo_screenheight()
  69. a = random.randrange(0, width)
  70. b = random.randrange(0, height)
  71. window.title(title)
  72. window.geometry("300x50" + "+" + str(a) + "+" + str(b))
  73. tk.Label(window,
  74. text=text, # 标签的文字
  75. # bg='white', # 背景颜色
  76. font=('楷体', 17), # 字体和字体大小
  77. width=15, height=2 # 标签长宽
  78. ).pack() # 固定窗口位置
  79. window.mainloop()
  80.  
  81. #触发弹窗炸弹
  82. def open_start():
  83. threads = []
  84. for i in range(50): # 需要的弹框数量
  85. t = threading.Thread(target=dow)
  86. t.setDaemon(True)
  87. threads.append(t)
  88. time.sleep(0.1)
  89. threads[i].start()
  90.  
  91. # 新建无法直接关闭的TK类
  92. class NewTk(tk.Tk):
  93. #重写“X”按钮
  94. def destroy(self):
  95. root = tk.Tk()
  96. root.withdraw()
  97. for i in range(3):
  98. if i == 0:
  99. showinfo(title="title1", message="question1")
  100. if i == 1:
  101. showinfo(title="title2", message="question2")
  102. if i == 2:
  103. showinfo(title="title3", message="question3")
  104. global text
  105. text = "title"
  106. global title
  107. title = "text"
  108. open_start()
  109.  
  110. #主程序体
  111. thread = threading.Thread(target=changeVd)
  112. thread.setDaemon(True)
  113. thread.start()
  114. ScreenChange()
  115. win = NewTk()
  116. win.title(title)
  117. win.geometry("%sx%s+%s+%s" % (WINWIDTH, WINHEIGHT, WINX, WINY))#样式设置
  118. win.resizable(0, 0)#阻止窗口大小化
  119.  
  120. photo = tk.PhotoImage(file="lib/没钱了.gif")#这里替换成你的图片地址
  121. imgLabel = tk.Label(win, image=photo)#将图片添加至窗口
  122. imgLabel.place(x=img_x, y=img_y)#设置图片位置
  123.  
  124. question_text = tkFont.Font(size=20, weight=tkFont.BOLD)
  125. q = tk.Label(win, text=question, font=question_text)
  126. q.place(x=question_x, y=question_y)
  127.  
  128. #OK按钮点击事件
  129. def click_yes():
  130. Start_music()
  131. root = tk.Tk()
  132. root.withdraw()
  133. for i in range(5):
  134. if i == 0:
  135. showinfo(title="title1", message="text1")
  136. if i == 1:
  137. showinfo(title="title2", message="text2")
  138. if i == 2:
  139. showinfo(title="title3", message="text3")
  140. global text
  141. text = "text"
  142. global title
  143. title = "title"
  144. open_start()
  145. if i == 3:
  146. print("运行退出程序")
  147. sys.exit(0)
  148.  
  149. yes_button = tk.Button(win, text=yes, command=click_yes)
  150. yes_button.place(x=yes_button_x, y=button_y, width=button_width, height=button_height)
  151.  
  152. no_button = tk.Button(win, text=no)
  153. no_button.place(x=no_button_x, y=button_y, width=button_width, height=button_height)
  154.  
  155. #NO按钮触碰事件
  156. def mouse_in_no_click(event):
  157. bx, by = random.randint(button_width, WINWIDTH-button_width), random.randint(button_height, WINHEIGHT-button_height)
  158. no_button.place(x=bx, y=by)
  159.  
  160. no_button.bind("<Motion>", mouse_in_no_click)
  161. win.mainloop()

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

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

原文链接:https://blog.csdn.net/botellei/article/details/111413536