Skip to content

Commit daa7d4d

Browse files
committed
增加了进度条创建时会循环寻找窗口句柄
1 parent 3b46f9b commit daa7d4d

4 files changed

Lines changed: 28 additions & 6 deletions

File tree

assets.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,3 @@ def background(self) -> PhotoImage:
2020
for x in range(BG_COUNT):
2121
bg.paste(image, (x * width, y * height))
2222
return PhotoImage(bg)
23-

main_gui.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
from base64 import b64decode, b64encode
1313
from pyperclip import copy as copy_clipboard
1414
from ttkbootstrap.scrolled import ScrolledFrame
15+
from comtypes import CoInitialize, CoUninitialize
1516
from time import perf_counter, sleep, time, strftime
1617
from pickle import loads as pickle_loads, dumps as pickle_dumps
1718
from json import load as json_load, dump as json_dump, JSONDecodeError
1819
from win32con import MB_ICONWARNING, MB_YESNOCANCEL, IDYES, MB_ICONERROR, MB_OK, MB_YESNO, MB_ICONQUESTION, IDNO, \
1920
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
2123

2224
DEBUG = "debug"
2325

@@ -822,7 +824,7 @@ def __init__(self, master: Misc, logger: Logger, server_list: ServerList):
822824
self.progress_var = 0
823825
self.callback_workers = 0
824826
self.taskbar = None
825-
self.after(100, self.taskbar_create)
827+
Thread(target=self.taskbar_create, daemon=True).start()
826828

827829
# 进度条
828830
self.progress_bar = InfoProgressBar(self, interval=0.05, text="扫描进度: ")
@@ -861,8 +863,29 @@ def __init__(self, master: Misc, logger: Logger, server_list: ServerList):
861863
self.stop_button.pack(fill=X, expand=True, pady=2)
862864

863865
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)
865887
self.start_button.configure(state=NORMAL)
888+
CoUninitialize()
866889

867890
def callback(self, info: Any):
868891
if not self.in_scan:

widgets.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ def __init__(self, master: Misc, tip: str, value: List[str] = None):
137137
self.text = ttk.Label(self, text=tip)
138138
self.combobox = ttk.Combobox(self, values=value)
139139

140-
self.combobox.current(0)
140+
if len(value) > 0:
141+
self.combobox.current(0)
141142

142143
self.text.pack(side=LEFT)
143144
self.combobox.pack(side=LEFT, fill=X, expand=True)

win_tool.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from enum import Enum
22
import comtypes.client as cc
3-
from win32con import FLASHW_ALL
43
from win32gui import FlashWindowEx
54
cc.GetModule(r"assets\TaskbarLib.tlb")
65
import comtypes.gen.TaskbarLib as TaskbarLib

0 commit comments

Comments
 (0)