微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Tkinter 滚动条冻结 GUI

如何解决Tkinter 滚动条冻结 GUI

这个项目的目的是加载所有文件名和一个下载按钮,用于从用户提供的 URL 下载文件

我有一个运行的 python 脚本,但是当我按下向下箭头滚动结果时,GUI 冻结。我想解决这个问题,以便在向下箭头按下时 GUI 不会冻结按下。

我对python很陌生,所以可能犯了一些初学者错误

这是一个使用 tkinter 和 chromedriver.exe 的 python GUI

import tkinter as tk
import tkinter.font as font
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import Chrome,ChromeOptions
from selenium.webdriver.chrome.options import Options
from threading import Thread
import webbrowser
import time
from tkinter import *
root = Tk()
root.geometry("640x480")
root.state("zoom")

tk.Label(root,text="URL:",font=font.Font(size=20)).place(x=20,y=20)
url_input = Entry(root,font=font.Font(size=20))
url_input.place(x=20,y=60,width=490,height=40)

global result_frame
urls = []

main_frame = LabelFrame(root)

canvas = tk.Canvas(main_frame,highlightbackground="gray")
canvas.pack(side=LEFT,fill="both",expand="yes")

v_scroll_bar = tk.Scrollbar(main_frame,orient="vertical",command=canvas.yview)
v_scroll_bar.pack(side=RIGHT,fill="y")

canvas.configure(yscrollcommand=v_scroll_bar.set)
canvas.bind("<Configure>",lambda e : canvas.configure(scrollregion=canvas.bBox("all")))

result_frame = tk.Frame(canvas)
result = []
result_b = []

canvas.create_window((0,0),window=result_frame,anchor="nw")
main_frame.pack(fill="both",expand="yes")

def download_links(links) :
    global result_frame
    global urls
    urls = links
    for l in links :
        lLabel = tk.Label(result_frame,font=font.Font(size=20),text=str(links.index(l)+1)+"."+l.find_elements_by_css_selector("*")[0].get_attribute("title"))
        lLabel.grid(row=links.index(l),column=0,sticky=tk.W)

        lDownloadB = tk.Button(result_frame,text="Download")
        lDownloadB.grid(row=links.index(l),column=1,sticky=tk.E,ipadx=10,ipady=5)
        def dl(e) :
            durl = "https://ps4.td-index.workers.dev/0:down/_Alvro%20Collection/"+urls[result_b.index(e.widget)].find_elements_by_css_selector("*")[0].get_attribute("title")
            print(durl)
            webbrowser.open_new(durl)
        lDownloadB.bind("<ButtonRelease-1>",lambda e : dl(e))

        result.append(lLabel)
        result_b.append(lDownloadB)
        main_frame.pack(fill=tk.BOTH,expand=1)
        canvas.pack(side=tk.LEFT,fill=tk.BOTH,expand=1)
def res () :
    while True :
        time.sleep(0.0000000001)
        canvas.configure(scrollregion=canvas.bBox("all"))
t = Thread(target=res)
t.daemon = True
t.start()

def scrap() :
    global d
    if not url_input.get().strip().replace("\n","") == "" :
        options = Options()
        options.headless = True
        d = Chrome(executable_path="chromedriver.exe",options=options)
        d.get(url_input.get())
        def func() :
            time.sleep(2)
            while True :
                time.sleep(0.00001)
                links = d.find_elements_by_css_selector("#app > div > section > div > div > div.golist > table > tbody > tr")
                if len(links) > 50 :
                    t = Thread(target=lambda:download_links(links))
                    t.daemon = True
                    t.start()
                    break
                else :
                    d.refresh()
        t = Thread(target=func)
        t.daemon = True
        t.start()
        print(d.find_elements_by_css_selector("#app > div > section > div > div > div.golist > table > tbody > tr"))

scarp_b = tk.Button(root,text="Get",command=scrap)
scarp_b.place(x=510,width=100,height=40)

def conf() :
    url_input.place(x=20,width=root.winfo_width()-150,height=40)
    scarp_b.place(x=root.winfo_width()-120,height=40)
    main_frame.place(x=0,y=140,width=root.winfo_width(),height=root.winfo_height()-140)
root.bind("<Configure>",lambda e : conf())
root.mainloop()

运行这个脚本时它看起来像。

当我输入这个链接Please copy this link)并按下GET按钮时,它会延迟一两分钟,然后显示数据。但是当我尝试按下向下箭头时,整个 GUI 都冻结了。

GUI Image

现在是按下GET按钮的时候了: 您可以看到此视频无效。

Youtube Link

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。