Search code examples
python-3.xtkinterpython-webbrowser

Tkinter says "Not Responding" until webbrower is closed


Tkinter says "Not Responding" until webbrowser is closed is it possible to fix with multi-threading? i have added the code here beat() has the webbrowser and i am calling them from solve() of button click I have removed some code as it says" It looks like your post is mostly code; please add some more details."

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()

The problem is that if I call beat() in another thread still the problem persists maybe due to solve() includes beat().And forgot to say that this uses sympy to solve and then mathjax to display result is chrome by webbrowser


Solution

  • Define a normal python function to do the webbrowser things like this:

    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()
    

    and then you can call it using:

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

    That starts a new thread where the start_webbrowser function is called with cwd as the only argument. For more info on python threading read this