|
12 | 12 | from base64 import b64decode, b64encode |
13 | 13 | from pyperclip import copy as copy_clipboard |
14 | 14 | from ttkbootstrap.scrolled import ScrolledFrame |
| 15 | +from comtypes import CoInitialize, CoUninitialize |
15 | 16 | from time import perf_counter, sleep, time, strftime |
16 | 17 | from pickle import loads as pickle_loads, dumps as pickle_dumps |
17 | 18 | from json import load as json_load, dump as json_dump, JSONDecodeError |
18 | 19 | from win32con import MB_ICONWARNING, MB_YESNOCANCEL, IDYES, MB_ICONERROR, MB_OK, MB_YESNO, MB_ICONQUESTION, IDNO, \ |
19 | 20 | IDCANCEL |
20 | | -from win32gui import MessageBox, FindWindow, FindWindowEx, GetParent, EnumChildWindows, GetWindowText, SetWindowText |
| 21 | +from sys import stderr |
| 22 | +from win32gui import MessageBox, FindWindow, FindWindowEx, GetParent, EnumChildWindows, GetWindowText, SetWindowText, GetClassName |
21 | 23 |
|
22 | 24 | DEBUG = "debug" |
23 | 25 |
|
@@ -822,7 +824,7 @@ def __init__(self, master: Misc, logger: Logger, server_list: ServerList): |
822 | 824 | self.progress_var = 0 |
823 | 825 | self.callback_workers = 0 |
824 | 826 | self.taskbar = None |
825 | | - self.after(100, self.taskbar_create) |
| 827 | + Thread(target=self.taskbar_create, daemon=True).start() |
826 | 828 |
|
827 | 829 | # 进度条 |
828 | 830 | self.progress_bar = InfoProgressBar(self, interval=0.05, text="扫描进度: ") |
@@ -861,8 +863,29 @@ def __init__(self, master: Misc, logger: Logger, server_list: ServerList): |
861 | 863 | self.stop_button.pack(fill=X, expand=True, pady=2) |
862 | 864 |
|
863 | 865 | def taskbar_create(self): |
864 | | - self.taskbar = TaskbarApi(FindWindow("TkTopLevel", "MC服务器扫描器")) |
| 866 | + CoInitialize() |
| 867 | + timeout = 2.0 |
| 868 | + timer = perf_counter() |
| 869 | + main_window = 0 |
| 870 | + while perf_counter() - timer < timeout: |
| 871 | + hwnd = self.winfo_id() |
| 872 | + while True: |
| 873 | + parent = GetParent(hwnd) |
| 874 | + if parent == 0: |
| 875 | + break |
| 876 | + else: |
| 877 | + hwnd = copy(parent) |
| 878 | + if GetWindowText(hwnd) == "MC服务器扫描器" and GetClassName(hwnd) == "TkTopLevel": |
| 879 | + self.taskbar = TaskbarApi(hwnd) |
| 880 | + main_window = copy(hwnd) |
| 881 | + break |
| 882 | + sleep(0.05) |
| 883 | + else: |
| 884 | + print("main_gui.taskbar_create: Main Window Not Found!", file=stderr) |
| 885 | + print(f"Done! Found main window, use: {round((perf_counter() - timer) * 1000, 2)} ms, hWnd: {main_window}") |
| 886 | + self.taskbar = TaskbarApi(main_window) |
865 | 887 | self.start_button.configure(state=NORMAL) |
| 888 | + CoUninitialize() |
866 | 889 |
|
867 | 890 | def callback(self, info: Any): |
868 | 891 | if not self.in_scan: |
|
0 commit comments