Python使用pycharm實(shí)現(xiàn)無(wú)限彈窗程序
運(yùn)行環(huán)境
PyCharm 2023.2.1
python3.11
具體內(nèi)容
源代碼
import tkinter as tk
from tkinter import messagebox
import threading
class PopupGenerator:
def __init__(self):
self.root = tk.Tk()
self.root.geometry("200x120")
self.root.title("無(wú)限彈窗")
self.root.protocol("WM_DELETE_WINDOW", lambda: None) # 用戶(hù)不可關(guān)閉彈窗一
self.common_style = {"font": ("華文新魏", 14)}
self.label = tk.Label(self.root, text="恭喜你打開(kāi)了這個(gè)程序", **self.common_style, fg="red")
self.label.pack(pady=20)
self.close_program_button = tk.Button(self.root, text="關(guān)閉程序", command=self.try_detox, **self.common_style,
bg="green", fg="white")
self.close_program_button.pack(pady=10)
self.popup_count = 0
self.detox_attempts = 0
self.popup_positions = [] # 存儲(chǔ)已存在彈窗的位置信息
self.generate_popup()
def generate_popup(self):
if self.popup_count < 20:
popup = tk.Toplevel(self.root)
popup.title("無(wú)限彈窗")
popup.geometry("200x120")
# 檢查已存在彈窗的位置,設(shè)置新彈窗的位置
x, y = self.calculate_popup_position(popup.winfo_reqwidth(), popup.winfo_reqheight())
popup.geometry(f"+{x}+{y}")
popup_label = tk.Label(popup, text="多試一下", fg="blue", **self.common_style)
popup_label.pack(pady=20)
popup.protocol("WM_DELETE_WINDOW", self.on_popup_close)
self.popup_count += 1
threading.Timer(1, self.generate_popup).start()
def calculate_popup_position(self, width, height):
# 計(jì)算新彈窗的位置,避免重疊
x_offset, y_offset = 25, 25
x = self.root.winfo_x() + x_offset + len(self.popup_positions) * x_offset
y = self.root.winfo_y() + y_offset + len(self.popup_positions) * y_offset
# 存儲(chǔ)新彈窗的位置信息
self.popup_positions.append((x, y))
return x, y
def on_popup_close(self):
self.generate_additional_popup()
def generate_additional_popup(self):
additional_popup = tk.Toplevel(self.root)
additional_popup.title("無(wú)限彈窗")
additional_popup.geometry("200x120")
# 檢查已存在彈窗的位置,設(shè)置新彈窗的位置
x, y = self.calculate_popup_position(additional_popup.winfo_reqwidth(), additional_popup.winfo_reqheight())
additional_popup.geometry(f"+{x}+{y}")
additional_popup_label = tk.Label(additional_popup, text="并沒(méi)有用", fg="purple", **self.common_style)
additional_popup_label.pack(pady=20)
def try_detox(self):
self.detox_attempts += 1
if self.detox_attempts <= 10:
messagebox.showinfo("溫馨提示", f"你不會(huì)覺(jué)得點(diǎn)了 {self.detox_attempts} 次就有用吧")
else:
messagebox.showinfo("沒(méi)想到啊", f"你居然堅(jiān)持點(diǎn)了 {self.detox_attempts}次")
self.root.destroy()
if __name__ == "__main__":
popup_generator = PopupGenerator()
popup_generator.root.mainloop()運(yùn)行結(jié)果
如下圖
點(diǎn)擊運(yùn)行時(shí)

點(diǎn)擊關(guān)閉程序按鈕(未點(diǎn)擊足夠次數(shù))

點(diǎn)擊關(guān)閉程序按鈕(點(diǎn)擊次數(shù)足夠)

關(guān)閉多試一下彈窗(僅并沒(méi)有用彈窗可關(guān)閉)

關(guān)閉程序的方法
1、任務(wù)管理器結(jié)束任務(wù)
2、點(diǎn)擊足夠次數(shù)的關(guān)閉程序
注意事項(xiàng)
1、沒(méi)有什么技術(shù)含量,僅娛樂(lè)使用
到此這篇關(guān)于Python使用pycharm實(shí)現(xiàn)無(wú)限彈窗程序的文章就介紹到這了,更多相關(guān)Python pycharm無(wú)限彈窗內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python臨時(shí)文件創(chuàng)建之tempfile模塊介紹
這篇文章主要介紹了Python臨時(shí)文件創(chuàng)建之tempfile模塊,Python的tempfile模塊是用來(lái)創(chuàng)建臨時(shí)文件或者文件夾的跨平臺(tái)工具,下面關(guān)于模塊簡(jiǎn)單介紹需要的小伙伴可以參考一下2022-03-03
python使用正則表達(dá)式匹配txt特定字符串(有換行)
這篇文章主要給大家介紹了關(guān)于python使用正則表達(dá)式匹配txt特定字符串的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Django通過(guò)腳本上傳文件的詳細(xì)操作指南
文件上傳是一個(gè)比較常用的網(wǎng)站功能,在服務(wù)器端,Django會(huì)使用一個(gè)叫作request.FILES的對(duì)象來(lái)處理上傳的文件,本文給大家介紹了Django通過(guò)腳本上傳文件的詳細(xì)操作指南,需要的朋友可以參考下2025-07-07
使用python腳本自動(dòng)創(chuàng)建pip.ini配置文件代碼實(shí)例
這篇文章主要介紹了使用python腳本自動(dòng)創(chuàng)建pip.ini配置文件代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
OpenCV圖像修復(fù)cv2.inpaint()的使用
這篇博客將介紹如何通過(guò)OpenCV中圖像修復(fù)的技術(shù)——cv2.inpaint() 去除舊照片中的小噪音、筆劃等。并提供一個(gè)可交互式的程序,感興趣的可以了解一下2021-08-08
Python中逗號(hào)轉(zhuǎn)為空格的三種方法
本文介紹了Python中將逗號(hào)轉(zhuǎn)換為空格的三種方法,包含使用replace函數(shù)、使用split函數(shù)、使用正則表達(dá)式,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
python中shape[0]與shape[1]的說(shuō)明
這篇文章主要介紹了python中shape[0]與shape[1]的說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08

