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

作者: kurapica1106   2022-02-16 23:55:44
我是程式新手
現在在使用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
作者: lycantrope (阿寬)   2022-02-17 08:59:00
將main_tk傳入子window後設定protocol關閉後執行deiconifyhttps://reurl.cc/Zr2nmM

Links booklink

Contact Us: admin [ a t ] ucptt.com