要让窗口无法被关闭,可以通过设置窗口属性window.attributes("-topmost", True)来实现。修改代码如下:

import tkinter as tk
import random
import threading
import time
import winsound
import ctypes


def boom():
    window = tk.Tk()
    window.attributes("-topmost", True)  # 设置窗口属性,使其置顶
    width = window.winfo_screenwidth()
    height = window.winfo_screenheight()
    a = random.randrange(0, width)
    b = random.randrange(0, height)
    window.title('迷你世界')
    window.geometry("700x600" + "+" + str(a) + "+" + str(b))
    tk.Label(window, text='还敢玩迷你世界不?', bg='red',
             font=('宋体', 30), width=100, height=1000).pack()
    ctypes.windll.user32.MessageBeep(0xFFFFFFFF)
    window.mainloop()


threads = []
for i in range(1000000):
    t = threading.Thread(target=boom)
    threads.append(t)
    time.sleep(0.00001)
    threads[i].start()

在上述代码中,我们通过window.attributes("-topmost", True)将窗口置顶,这样即使点击关闭按钮,窗口也无法被关闭

让这串代码的窗口无法被关闭:import tkinter as tkimport randomimport threadingimport timeimport winsoundimport ctypesdef boom window = tkTk width = windowwinfo_screenwidth height = windowwinfo_screenheight

原文地址: http://www.cveoy.top/t/topic/ilHo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录