[問題] tkinter使用不同class後如何互相呼叫

看板Python作者時間3年前 (2022/02/16 23:55), 3年前編輯推噓1(102)
留言3則, 1人參與, 3年前最新討論串1/1
我是程式新手 現在在使用tkinter想寫一個螢幕截圖的工具 我是把兩個tkinter視窗分別寫進兩個class import tkinter class window_main(): def __init__(self): self.window = tkinter.Tk() self.set_window_title("Screen Shot") self.create_widgets() self.window.mainloop() def set_window_title(self, title): self.window.title(title) def create_widgets(self): self.button_st = tkinter.Button(self.window) self.button_st["text"] = "Screen Shot" self.button_st["height"] = 2 self.button_st["width"] = 13 self.button_st["command"] = self.screen_shot self.button_st.grid(row = 0, column = 0, padx = 5, pady = 5) self.button_exit = tkinter.Button(self.window) self.button_exit["text"] = "Exit" self.button_exit["height"] = 2 self.button_exit["width"] = 13 self.button_exit["command"] = self.event_exit self.button_exit.grid(row = 1, column = 0, padx = 5, pady = 5) def wind_hide(self): self.window.withdraw() def wind_show(self): self.window.deiconify() def screen_shot(self): self.wind_hide() self.sub_app = window_full_screen() def event_exit(self): self.window.destroy() class window_full_screen(): def __init__(self): self.window_exist = True self.window = tkinter.Tk() self.window.attributes("-fullscreen", True) self.window.attributes("-alpha", 0.25) self.create_canvas() self.window.bind("<Escape>", self.event_exit) self.window.mainloop() def create_canvas(self): self.canvas = tkinter.Canvas(self.window, width = self.window.winfo_screenwidth(), height = self.window.winfo_screenheight()) self.canvas.place(x = 0, y = 0) self.canvas.create_line(0, 0, 1440, 900) def event_exit(self, event): self.window_exist = False self.window.destroy() app = window_main() 當window_main打開window_full_screen的時候 我想把window_main給隱藏 使用withdraw 但當我把window_full_screen關閉後 我目前想不到要如何把window_main叫回來 雖然我知道要使用deiconify 但要如何在window_full_screen觸發關閉後 讓window_main也能觸發事件 程式碼原始檔 https://github.com/kurapica1106/screenshot_and_compare/ blob/screenshot/screenshot_2022_02_16_01_.py 縮網址 https://reurl.cc/xOKYyz -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.250.116.118 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1645026948.A.40F.html

02/17 08:59, 3年前 , 1F
將main_tk傳入子window後設定protocol關閉後執行
02/17 08:59, 1F

02/17 08:59, 3年前 , 2F
deiconify
02/17 08:59, 2F

02/17 08:59, 3年前 , 3F
感謝大大的程式碼 原來class可以這樣傳遞上一層class 學到一課 ※ 編輯: kurapica1106 (114.136.61.69 臺灣), 02/17/2022 12:12:28
文章代碼(AID): #1Y3Hw4GF (Python)
文章代碼(AID): #1Y3Hw4GF (Python)