簡體   English   中英

Python 和關閉窗口/功能和 tkinter

[英]Python and closing windows/functions and tkinter

我試圖了解如何關閉我的腳本中的東西,但是如果我制作一個框架來包含 tkinter 的小部件和東西,當我運行代碼時,屏幕上不會打印 object ......所以,將小部件放在根目錄 window 中似乎成為必選項。 作為初學者,我發現命令關閉 function 的唯一方法是:

def read_note():
    def close_readnote():
        text_box.destroy()
        lbl_readnote.destroy()
        btn_close_readnote.destroy()

root.title("Reader of something")
lbl_readnote = Label(root, text="Leggi note", font=20)
lbl_readnote.pack(pady=25)

text_box = Text(root)
text_box.pack(expand=True)
text_box.config(state='disabled')

btn_close_readnote = Button(root, text="x", command=close_readnote)
btn_cls_readnote.pack(side=RIGHT)

root.mainloop()

正確的方法是如何做到這一點?

如果我理解你的問題,這里有兩種我用來關閉 windows 的方法:

關閉 function:

# Closing command
def on_closing(self):
    if messagebox.askokcancel("Quit", "Do you want to quit?"):
        self.destroy()

使用“退出”按鈕:

quit_button = tk.Button(self, text="Quit", command=self.on_closing)
quit_button.grid(row=10, column=4, padx=5, pady=5, sticky='E')

使用小部件右上角的關閉按鈕詢問用戶是否要退出:

# Tkinter protocol for x close
self.protocol("WM_DELETE_WINDOW", self.on_closing)

這是使用面向對象的編程,我發現這對 tkinter 編程很有幫助,但您可以刪除自引用。 您可以在 class 中包含這兩種方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM