Tkinter 说 "Not Responding" 直到 webbrower 关闭

Tkinter says "Not Responding" until webbrower is closed

Tkinter 在关闭网络浏览器之前显示“无响应” 可以用多线程修复吗? 我在这里添加了代码 beat() 有网络浏览器 我从 solve() 给他们打电话 按钮点击 我已经删除了一些代码,因为它说“ 看起来你的 post 主要是代码;请添加更多详细信息。"

def solve():
 if com.get() == "one":  
    d=e1.get()
    if not len(d)==1:
       Msg=msg.showerror("Only one variable", "Equation must contain only one variable!")
    else: 
     clear()    
     
     t1.insert(tk.INSERT,"$$"+"Solutions\ of\ "+d+"\ are:"+"$$"+"<br>")
     for z in range (1,len(a2.args)+1):
         t1.insert(tk.INSERT,"$$"+d+"="+latex(((a2.args[z-1])))+"$$")
     beat()    
 elif com.get() == "two":
    d=e1.get()
    if not len(d)==3:
       Msg=msg.showerror("Two variables", "Equation must contain two variables seperated by comma!")
    else: 
       clear()
       
       abcd=nonlinsolve((bb2),[a,c])
       t1.insert(tk.INSERT,"$$"+"Solutions\ of\ "+str(x[0])+","+str(x[1])+"\ are:"+"$$"+"<br>")
       for z in range (1,len(abcd)+1):
         t1.insert(tk.INSERT,"$$"+str(x[0])+","+str(x[1])+"="+latex(((abcd.args[z-1])))+"$$")
       beat()  
 elif com.get() == "three":
    d=e1.get()
    if not len(d)==5:
       Msg=msg.showerror("Three variables", "Equation must contain three variables seperated by comma!")
    else: 
       clear()
       x=d.split(',')
       a=Symbol(x[0])
       c=Symbol(x[1])
       d=Symbol(x[2])
       bb=e2.get()
       bb1=bb.replace('=','-')
       bb2=mathematica(bb1)

       
       abcd=nonlinsolve(bb2,[a,c,d])
       t1.insert(tk.INSERT,"$$"+"Solutions\ of\ "+str(x[0])+","+str(x[1])+","+str(x[2])+"\ are:"+"$$"+"<br>")
       for z in range (1,len(abcd)+1):
         t1.insert(tk.INSERT,"$$"+str(x[0])+","+str(x[1])+","+str(x[2])+"="+latex(((abcd.args[z-1])))+"$$")
       beat()                
 elif com.get() == "four":
    d=e1.get()
    if not len(d)==7:
       Msg=msg.showerror("Four variables", "Equation must contain four variables seperated by comma!")
    else: 
       clear()
       x=d.split(',')
       a=Symbol(x[0])
       c=Symbol(x[1])
       d=Symbol(x[2])
       e=Symbol(x[3])
       bb=e2.get()
       bb1=bb.replace('=','-')
       bb2=mathematica(bb1)

       
       abcd=nonlinsolve(sympify(bb2),[a,c,d,e])
       t1.insert(tk.INSERT,"$$"+"Solutions\ of\ "+str(x[0])+","+str(x[1])+","+str(x[2])+","+str(x[3])+"\ are:"+"$$"+"<br>")
       for z in range (1,len(abcd)+1):
         t1.insert(tk.INSERT,"$$"+str(x[0])+","+str(x[1])+","+str(x[2])+","+str(x[3])+"="+latex(((abcd.args[z-1])))+"$$")
       beat()  
 elif com.get() == "five":
    d=e1.get()
    if not len(d)==9:
       Msg=msg.showerror("Five variables", "Equation must contain five variables seperated by comma!")
    else: 
       clear()
       x=d.split(',')
       a=Symbol(x[0])
       c=Symbol(x[1])
       d=Symbol(x[2])
       e=Symbol(x[3])
       f=Symbol(x[4])
       bb=e2.get()
       bb1=bb.replace('=','-')
       bb2=mathematica(bb1)
   
       
       abcd=nonlinsolve(sympify(bb2),[a,c,d,e,f])
       t1.insert(tk.INSERT,"$$"+"Solutions\ of\ "+str(x[0])+","+str(x[1])+","+str(x[2])+","+str(x[3])+","+str(x[4])+"\ are:"+"$$"+"<br>")
       for z in range (1,len(abcd)+1):
         t1.insert(tk.INSERT,"$$"+str(x[0])+","+str(x[1])+","+str(x[2])+","+str(x[3])+","+str(x[4])+"="+latex(((abcd.args[z-1])))+"$$")
       beat()  
def clear():
    t1.delete(1.0,'end')
def beat():
 f = open("hmmmm.html", "w",encoding="utf-8")
 t1.update()
 s=((t1.get('1.0','end')))
 f.write("""<html>
 +"\n"+
 s""")
 f.close()
 cwd = os.getcwd()
 webbrowser.get('"C:\Program Files\Google\Chrome\Application\chrome.exe" %s').open(cwd+'/'+'hmmmm.html')
 webbrowser.close()

root=tk.Tk()
root.config(background='#F0F8FF')
root.geometry("+255+45")
root.geometry("1000x750")
root.title("Equation solver")
w=tk.Label(root,text="Enter variables(separated by comma):",bg='#F0F8FF')
w.place(x=81,y=119)
w1=tk.Label(root,text="No.of variables:",bg='#F0F8FF')
w1.place(x=83,y=42)
w2=tk.Label(root,text="Enter equation to solve:\n(separated by comma)",bg='#F0F8FF')
w2.place(x=81,y=179)
e2=tk.Entry(root,width=60)
e2.place(x=218,y=179)
e1=tk.Entry(root,width=50)
e1.place(x=280,y=119)
btn=tk.Button(root,text="Solve",bg='#BEF18B',command=solve)
btn.place(x=276,y=249)
btn1=tk.Button(root,text="Clear",bg='white',command=clear)
btn1.place(x=398,y=669)
t1=tt.ScrolledText(root,height=20,width=110)
t1.place(x=81,y=300)
n = tk.StringVar()
com=ttk.Combobox(root,width=12,textvariable=n)
com['values']=('one',
               'two',
               'three',
               'four',
               'five')
com.place(x=278,y=42)
root.mainloop()

问题是,如果我在另一个线程中调用 beat(),问题仍然存在,可能是由于 solve() includes beat()。忘了说这使用 sympy 来解决,然后使用 mathjax 来显示结果是 chrome 通过网络浏览器

定义一个正常的 python 函数来执行 webbrowser 像这样的事情:

from threading import Thread

def start_webbrowser(cwd):
    webbrowser.get('"C:\Program Files\Google\Chrome\Application\chrome.exe" %s').open(cwd+'/'+'hmmmm.html')
    webbrowser.close()

然后您可以使用以下方式调用它:

webbrowser_thread = Thread(target=start_webbrowser, args=(cwd, ), daemon=True)
webbrowser_thread.start()

这将启动一个新线程,其中 start_webbrowser 函数被调用,cwd 作为唯一参数。有关 python 线程的更多信息,请阅读 this