Skip to content

Commit

Permalink
Decouple the progressbar popup
Browse files Browse the repository at this point in the history
  • Loading branch information
kuankuan2007 committed Dec 2, 2024
1 parent 24aa92b commit 144f52e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 48 deletions.
61 changes: 61 additions & 0 deletions lib/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,64 @@ def close():

ttk.Button(buttonBox, text="确定", command=confirmed).grid(row=0, column=1, padx=10)
ttk.Button(buttonBox, text="取消", command=close).grid(row=0, column=0, padx=10)


def showProgress(
title: str,
progressName: List[str],
_cancel: Callable[[], None],
master: tk.Tk | tk.Toplevel = util.rootWindow,
length: int = 300,
) -> Tuple[
Callable[[], None],
Tuple[Callable[[], None], Callable[[], None]],
Tuple[ttk.Progressbar, ...],
]:
logger = util.getLogger("showProgress")
window = util.showModal(master)
window.title(title)

result: List[ttk.Progressbar] = []

for index, name in enumerate(progressName):
tk.Label(window, text=name).grid(
row=index, column=0, padx=5, pady=5, sticky="e"
)
progress = ttk.Progressbar(
window, orient="horizontal", mode="determinate", length=length
)
progress.grid(row=index, column=1, padx=5, sticky="w")
result.append(progress)

buttonBox = tk.Frame(window)
buttonBox.grid(row=len(progressName), column=0, columnspan=2, pady=5, sticky="e")

def ok():
"""
Callback function for the "OK" button.
Closes the window.
"""
close()
def _cancel():
"""
Callback function for the "Cancel" button.
Calls the `cancel` function and closes the window.
"""
_cancel()
close()
def close():
logger.info("window closed")
window.destroy()

okButton = ttk.Button(buttonBox, text="确定", command=ok, state=tk.DISABLED)
okButton.grid(row=0, column=1, padx=5)
ttk.Button(buttonBox, text="取消", command=_cancel).grid(row=0, column=0, padx=5)

return (
close,
(
lambda: okButton.config(state=tk.NORMAL),
lambda: okButton.config(state=tk.DISABLED),
),
tuple(result),
)
77 changes: 29 additions & 48 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,54 +70,28 @@ def startDownload(
audioPath = util.tempRoot.joinpath(f"audio-{time.time()}.tmp")

logger.info(f"videoPath: {videoPath}, audioPath: {audioPath}")

progressWindow = util.showModal(rootWindow)
progressWindow.title("下载进度")

_main = tk.Frame(progressWindow)
_main.grid(column=0, row=0, padx=10, pady=10)

tk.Label(_main, text="视频").grid(row=0, column=0, sticky="e")
tk.Label(_main, text="音频").grid(row=1, column=0, sticky="e")
tk.Label(_main, text="转码").grid(row=2, column=0, sticky="e")

videoProgress = ttk.Progressbar(
_main, orient="horizontal", mode="determinate", length=200
)
audioProgress = ttk.Progressbar(
_main, orient="horizontal", mode="determinate", length=200
)
mergeProgress = ttk.Progressbar(
_main, orient="horizontal", mode="indeterminate", length=200
)
mergeProgress.start()

videoProgress.grid(row=0, column=1)
audioProgress.grid(row=1, column=1)
mergeProgress.grid(row=2, column=1)

buttonBox = tk.Frame(_main)
buttonBox.grid(row=3, column=0, columnspan=2, sticky="E")

def cancel():
nonlocal cancelFlag
logger.info("download cancel")
for i in [videoThread, audioThread, mergeThread]:
try:
i._stop()
except Exception:
pass
close()
if videoThread is not None:
videoThread._stop()
if audioThread is not None:
audioThread._stop()
if mergeThread is not None:
mergeThread._stop()
logger.info("download cancel succeed")
cancelFlag = True

close, (canOK, _cannotOK), (videoProgress, audioProgress, mergeProgress) = (
util.dialog.showProgress("下载进度", ["视频", "音频", "转码"], cancel)
)

def close():
progressWindow.destroy()
logger.info("window closed")

ttk.Button(buttonBox, text="取消", command=cancel).grid(row=0, column=0)

okButton = ttk.Button(buttonBox, text="确定", state="disabled")
okButton.grid(column=1, row=0)
mergeProgress.config(mode="indeterminate")
mergeProgress.start()

succeed = 0
cancelFlag = False

def downloadSuccess():
nonlocal succeed
Expand All @@ -128,6 +102,8 @@ def downloadSuccess():

def fail(t=Literal["video", "audio"]):
logger.info(f"{t} download failed")
if cancelFlag:
return
if messagebox.askokcancel("错误", f"{t}下载失败,是否重试"):
logger.info(f"{t} download retry")
_start(t)
Expand All @@ -136,9 +112,9 @@ def fail(t=Literal["video", "audio"]):
cancel()

def mergeSuccess():
close()
canOK()
util.dialog.showinfo("下载完成", "下载完成")
logger.info("merge succeed, download complete")
messagebox.showinfo("完成", "下载完成")

def mergeFail():
logger.info("merge failed")
Expand Down Expand Up @@ -208,10 +184,15 @@ def askDownloadPart(

util.dialog.askToSelect(
"选择要下载的部分",
[(
"片段",
[f"{index+1}. {value['title']}" for index, value in enumerate(videoList)],
)],
[
(
"片段",
[
f"{index+1}. {value['title']}"
for index, value in enumerate(videoList)
],
)
],
callback=lambda x: callback(videoList[x][0]),
)

Expand Down

0 comments on commit 144f52e

Please sign in to comment.