如何解决Tkinter 说“不响应”,直到 webbrowser 关闭
Tkinter 说“不响应”,直到浏览器关闭 可以用多线程修复吗? 我在这里添加了代码 beat() 有浏览器 我从solve()给他们打电话 按钮点击 我已经删除了一些代码,因为它说” 看起来您的帖子主要是代码;请添加更多详细信息。”
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,c,d])
t1.insert(tk.INSERT,"+str(x[1])+","+str(x[2])+"\ are:"+"$$"+"<br>")
for z in range (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),d,e])
t1.insert(tk.INSERT,"+str(x[2])+","+str(x[3])+"\ are:"+"$$"+"<br>")
for z in range (1,"+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),e,f])
t1.insert(tk.INSERT,"+str(x[3])+","+str(x[4])+"\ are:"+"$$"+"<br>")
for z in range (1,"+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显示结果是webbrowser的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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。