您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關Pycharm如何制作搞怪彈窗的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
用python制作了一個搞怪的桌面彈窗程序,驚喜連連哦
運行動圖
實現代碼:
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如何制作搞怪彈窗”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。