日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python 关闭窗口事件_关于python:如何在Tkinter中处理窗口关闭事件?

發(fā)布時間:2023/12/10 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 关闭窗口事件_关于python:如何在Tkinter中处理窗口关闭事件? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

如何在Python Tkinter程序中處理窗口關(guān)閉事件(用戶單擊" X"按鈕)?

Tkinter支持一種稱為協(xié)議處理程序的機(jī)制。在這里,術(shù)語協(xié)議是指應(yīng)用程序和窗口管理器之間的交互。最常用的協(xié)議稱為WM_DELETE_WINDOW,用于定義當(dāng)用戶使用窗口管理器顯式關(guān)閉窗口時發(fā)生的情況。

您可以使用protocol方法為此協(xié)議安裝處理程序(小部件必須是Tk或Toplevel小部件):

這里有一個具體的例子:

import tkinter as tk

from tkinter import messagebox

root = tk.Tk()

def on_closing():

if messagebox.askokcancel("Quit","Do you want to quit?"):

root.destroy()

root.protocol("WM_DELETE_WINDOW", on_closing)

root.mainloop()

我使用了類似的代碼,但root.destroy()

如果您使用的是像Twisted這樣的東西來獨(dú)立維護(hù)事件循環(huán)或Tkinter(例如:twists反應(yīng)堆對象),請確保外部主循環(huán)已停止,并且為此目的提供了任何專門的技術(shù)(例如:twisted.reactor.stop())

在Windows上的Python 2.7上,Tkinter沒有子模塊消息框。 我使用了import tkMessageBox as messagebox

我認(rèn)為您應(yīng)該知道是您從其他人/其他地方復(fù)制了此答案和代碼。

我不知道,那不是我最初發(fā)布的代碼。

不為我工作。 硬關(guān)閉窗口時(例如使用Alt + F4),它不會改變經(jīng)典的Python對圖形中斷的混亂反應(yīng)。

@MitchMcMabers:另一方面,很長一段時間以來,tkinter幾乎沒有任何重大變化。

馬特展示了關(guān)閉按鈕的一種經(jīng)典修改。

另一種是使關(guān)閉按鈕最小化窗口。

您可以通過使用iconify方法來重現(xiàn)此行為

是協(xié)議方法的第二個參數(shù)。

這是一個在Windows 7上測試過的有效示例:

# Python 3

import tkinter

import tkinter.scrolledtext as scrolledtext

class GUI(object):

def __init__(self):

root = self.root = tkinter.Tk()

root.title('Test')

# make the top right close button minimize (iconify) the main window

root.protocol("WM_DELETE_WINDOW", root.iconify)

# make Esc exit the program

root.bind('', lambda e: root.destroy())

# create a menu bar with an Exit command

menubar = tkinter.Menu(root)

filemenu = tkinter.Menu(menubar, tearoff=0)

filemenu.add_command(label="Exit", command=root.destroy)

menubar.add_cascade(label="File", menu=filemenu)

root.config(menu=menubar)

# create a Text widget with a Scrollbar attached

txt = scrolledtext.ScrolledText(root, undo=True)

txt['font'] = ('consolas', '12')

txt.pack(expand=True, fill='both')

gui = GUI()

gui.root.mainloop()

在此示例中,我們?yōu)橛脩籼峁┝藘蓚€新的退出選項(xiàng):

經(jīng)典文件菜單->退出,還有Esc按鈕。

取決于Tkinter活動,尤其是在使用Tkinter.after時,使用destroy()停止此活動-即使使用protocol(),按鈕等-也會干擾此活動("執(zhí)行時出錯"),而不是只是終止它。在幾乎每種情況下,最好的解決方案是使用標(biāo)志。這是一個簡單,愚蠢的用法示例(盡管我敢肯定,你們大多數(shù)人都不需要它!

from Tkinter import *

def close_window():

global running

running = False

print"Window closed"

root = Tk()

root.protocol("WM_DELETE_WINDOW", close_window)

cv = Canvas(root, width=200, height=200)

cv.pack()

running = True;

# This is an endless loop stopped only by setting 'running' to 'False'

while running:

for i in range(200):

if not running:

break

cv.create_oval(i, i, i+1, i+1)

root.update()

這樣可以很好地終止圖形活動。您只需要在正確的位置檢查running。

嘗試簡單版本:

import tkinter

window = Tk()

closebutton = Button(window, text='X', command=window.destroy)

closebutton.pack()

window.mainloop()

或者,如果您想添加更多命令:

import tkinter

window = Tk()

def close():

window.destroy()

#More Functions

closebutton = Button(window, text='X', command=close)

closebutton.pack()

window.mainloop()

我要感謝Apostolos的回答,這一點(diǎn)引起了我的注意。這是2019年P(guān)ython 3的更加詳細(xì)的示例,帶有更清晰的描述和示例代碼。

注意以下事實(shí):destroy()(或根本沒有自定義窗口關(guān)閉處理程序)會在用戶關(guān)閉窗口時立即破壞該窗口及其所有正在運(yùn)行的回調(diào)。

這可能對您不利,這取決于您當(dāng)前的Tkinter活動,尤其是在使用tkinter.after(定期回調(diào))時。您可能正在使用處理一些數(shù)據(jù)并將其寫入磁盤的回調(diào)...在這種情況下,您顯然希望數(shù)據(jù)寫入完成而不會被突然終止。

最好的解決方案是使用標(biāo)志。因此,當(dāng)用戶請求關(guān)閉窗口時,可以將其標(biāo)記為一個標(biāo)志,然后對其進(jìn)行響應(yīng)。

(注意:我通常將GUI設(shè)計(jì)為封裝良好的類和單獨(dú)的工作線程,并且我絕對不使用"全局"(我使用類實(shí)例變量),但這只是一個簡單的示例,用于演示當(dāng)用戶關(guān)閉窗口時,Tk如何突然終止您的定期回調(diào)...)

from tkinter import *

import time

# Try setting this to False and look at the printed numbers (1 to 10)

# during the work-loop, if you close the window while the periodic_call

# worker is busy working (printing). It will abruptly end the numbers,

# and kill the periodic callback! That's why you should design most

# applications with a safe closing callback as described in this demo.

safe_closing = True

# ---------

busy_processing = False

close_requested = False

def close_window():

global close_requested

close_requested = True

print("User requested close at:", time.time(),"Was busy processing:", busy_processing)

root = Tk()

if safe_closing:

root.protocol("WM_DELETE_WINDOW", close_window)

lbl = Label(root)

lbl.pack()

def periodic_call():

global busy_processing

if not close_requested:

busy_processing = True

for i in range(10):

print((i+1),"of 10")

time.sleep(0.2)

lbl["text"] = str(time.time()) # Will error if force-closed.

root.update() # Force redrawing since we change label multiple times in a row.

busy_processing = False

root.after(500, periodic_call)

else:

print("Destroying GUI at:", time.time())

try: #"destroy()" can throw, so you should wrap it like this.

root.destroy()

except:

# NOTE: In most code, you'll wanna force a close here via

#"exit" if the window failed to destroy. Just ensure that

# you have no code after your `mainloop()` call (at the

# bottom of this file), since the exit call will cause the

# process to terminate immediately without running any more

# code. Of course, you should NEVER have code after your

# `mainloop()` call in well-designed code anyway...

# exit(0)

pass

root.after_idle(periodic_call)

root.mainloop()

該代碼將向您顯示W(wǎng)M_DELETE_WINDOW處理程序即使在我們的自定義periodic_call()忙于工作/循環(huán)的過程中仍在運(yùn)行!

我們使用一些非常夸張的.after()值:500毫秒。這只是為了使您很容易看到定期呼叫忙時關(guān)閉與否之間的區(qū)別...如果在數(shù)字更新時關(guān)閉,您將看到WM_DELETE_WINDOW在定期呼叫時發(fā)生呼叫"正在處理:True"。如果您在數(shù)字暫停時關(guān)閉(這意味著此時不處理定期回調(diào)),則會看到關(guān)閉發(fā)生在"不忙"期間。

在實(shí)際使用中,您的.after()將使用30到100毫秒之類的時間來具有響應(yīng)GUI。這只是一個演示,可以幫助您了解如何保護(hù)自己免受Tk的默認(rèn)"關(guān)閉時立即中斷所有工作"的行為。

總結(jié):使WM_DELETE_WINDOW處理程序設(shè)置一個標(biāo)志,然后在安全的情況下(當(dāng)您的應(yīng)用程序完成所有工作時)定期并手動檢查該標(biāo)志.destroy()窗口。

PS:您也可以使用WM_DELETE_WINDOW詢問用戶是否真的要關(guān)閉窗口;如果他們回答"否",則無需設(shè)置標(biāo)志。非常簡單您只需在WM_DELETE_WINDOW中顯示一個消息框,然后根據(jù)用戶的答案設(shè)置標(biāo)志即可。

使用closeEvent

def closeEvent(self, event):

# code to be executed

這個答案需要更多細(xì)節(jié)。 這條線放在哪里? 在我這邊擺弄一點(diǎn)似乎無法使它正常工作。

總結(jié)

以上是生活随笔為你收集整理的python 关闭窗口事件_关于python:如何在Tkinter中处理窗口关闭事件?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。