Tkinter GUI无响应

2024-10-01 15:30:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用一种被广泛接受的方法(暗示该方法是无缝的)制作一个GUI来解决工程设计问题

此方法的代码在独立运行时需要0.537909984588623秒(不是在tkinter中,而是在普通代码中),并且不会太复杂或混乱。当我尝试使用tkinter修改此代码以适应GUI时,在输入所有输入并选择一个按钮后,即使程序一直在后台运行,它也会变得无响应

另外,当我强制关闭GUI窗口时,jupyter内核就死了

以下是我的代码的简要概述:

from tkinter import *
from scipy.optimize import fsolve
import matplotlib
import numpy as np
import threading
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
matplotlib.use('TkAgg')
import math
class MyWindow():
    def __init__(self, win):
        self.lbl1=Label(win, text='Alpha')
        self.lbl2=Label(win, text='xd')
        self.lbl3=Label(win, text='xw')
        self.lbl4=Label(win, text='xf')
        self.lbl5=Label(win, text='q')
        self.lbl6=Label(win, text='Reflux Factor')
        self.lbl7=Label(win, text='Tray Efficiency')
        self.lbl8=Label(win, text='Total Number of Stages')
        self.lbl9=Label(win, text='Feed Stage')
        
        self.t1=Entry(bd=3)
        self.t2=Entry(bd=3)
        self.t3=Entry(bd=3)
        self.t4=Entry(bd=3)
        self.t5=Entry(bd=8)
        self.t6=Entry(bd=8)
        self.t7=Entry(bd=8)
        self.t8=Entry(bd=8)
        self.t9=Entry(bd=8)
        
        self.btn1=Button(win, text='Total Number of Stages ', command=self.stagesN)
        
        self.lbl1.place(x=100, y=80)
        self.t1.place(x=300, y=80)
        self.lbl2.place(x=100, y=130)
        self.t2.place(x=300, y=130)
        self.lbl3.place(x=100, y=180)
        self.t3.place(x=300, y=180)
        self.lbl4.place(x=100, y=230)
        self.t4.place(x=300, y=230)
        self.lbl5.place(x=100, y=280)
        self.t5.place(x=300, y=280)
        self.lbl6.place(x=100, y=330)
        self.t6.place(x=300, y=330)
        self.lbl7.place(x=100, y=380)
        self.t7.place(x=300, y=380)
        self.lbl8.place(x=800, y=130)
        self.t8.place(x=790, y=170)
        self.lbl9.place(x=800, y=210)
        self.t9.place(x=790, y=260)
        self.btn1.place(x= 500, y= 75)
        
        
    def originalEq(self,xa,relative_volatility):
        ya=(relative_volatility*xa)/(1+(relative_volatility-1)*xa)
        return ya

    def equilibriumReal(self,xa,relative_volatility,nm):
        ya=(relative_volatility*xa)/(1+(relative_volatility-1)*xa)
        ya=((ya-xa)*nm)+xa 
        return ya

    def equilibriumReal2(self,ya,relative_volatility,nm):
        a=((relative_volatility*nm)-nm-relative_volatility+1)
        b=((ya*relative_volatility)-ya+nm-1-(relative_volatility*nm))
        c=ya
        xa=(-b-np.sqrt((b**2)-(4*a*c)))/(2*a) 
        return xa
    
    def stepping_ESOL(self,x1,y1,relative_volatility,R,xd,nm):
        x2=self.equilibriumReal2(y1,relative_volatility,nm) 
        y2=(((R*x2)/(R+1))+(xd/(R+1))) 
        return x1,x2,y1,y2

    def stepping_SSOL(self,x1,y1,relative_volatility,\
    ESOL_q_x,ESOL_q_y,xb,nm):
        x2=self.equilibriumReal2(y1,relative_volatility,nm) 
        m=((xb-ESOL_q_y)/(xb-ESOL_q_x)) 
        c=ESOL_q_y-(m*ESOL_q_x) 
        y2=(m*x2)+c 
        return x1,x2,y1,y2

    def stagesN(self):
        relative_volatility=float(self.t1.get())
        nm=float(self.t7.get())
        xd=float(self.t2.get())
        xb=float(self.t3.get())
        xf=float(self.t4.get())
        q=float(self.t5.get())
        R_factor=float(self.t6.get())
        
        xa=np.linspace(0,1,100) 
        ya_og=self.originalEq(xa[:],relative_volatility) 
        ya_eq=self.equilibriumReal(xa[:],relative_volatility,nm) 

        x_line=xa[:] 
        y_line=xa[:]
    

        al=relative_volatility
        a=((al*q)/(q-1))-al+(al*nm)-(q/(q-1))+1-nm
        b=(q/(q-1))-1+nm+((al*xf)/(1-q))-(xf/(1-q))-(al*nm)
        c=xf/(1-q)

        if q>1:
            q_eqX=(-b+np.sqrt((b**2)-(4*a*c)))/(2*a)
        else: 
            q_eqX=(-b-np.sqrt((b**2)-(4*a*c)))/(2*a)
    
        q_eqy=self.equilibriumReal(q_eqX,relative_volatility,nm)
    

        theta_min=xd*(1-((xd-q_eqy)/(xd-q_eqX))) 
        R_min=(xd/theta_min)-1 
        R=R_factor*R_min 
        theta=(xd/(R+1)) 

        ESOL_q_x=((theta-(xf/(1-q)))/((q/(q-1))-((xd-theta)/xd)))
        
        ESOL_q_y=(ESOL_q_x*((xd-theta)/xd))+theta
   

        x1,x2,y1,y2=self.stepping_ESOL(xd,xd,relative_volatility,R,xd,nm)
        step_count=1 
        while x2>ESOL_q_x: 
            x1,x2,y1,y2=self.stepping_ESOL(x2,y2,relative_volatility,R,xd,nm)
            step_count+=1 
            

        feed_stage=step_count 
    
        x1,x2,y1,y2=self.stepping_SSOL(x1,y1,relative_volatility\
        ,ESOL_q_x,ESOL_q_y,xb,nm)
        step_count+=1
        while x2>xb: 
            x1,x2,y1,y2=self.stepping_SSOL(x2,y2,relative_volatility\
            ,ESOL_q_x,ESOL_q_y,xb,nm)
            
            step_count+=1 
        xb_actual=x2 
        stagesN=step_count-1
        self.t8.insert(END, str(stagesN))
        return
        
        
        
window=Tk()
mywin=MyWindow(window)
window.title('DColumn')
window.geometry("1500x1500")
window.mainloop()

我在其他文章中读到,使用多线程可以降低mainloop的负载并防止冻结。但是就像我说的,代码不是很复杂。还是因为主循环上运行的所有东西?还是有什么东西比我们看到的要多?多线程是超越这一点的唯一途径吗


Tags: textimportselfplacewinx2nmrelative
1条回答
网友
1楼 · 发布于 2024-10-01 15:30:10

代码看起来不像你说的那么简单,因为使用了matpotlib和其他模块,它可能没有响应

如果让Tkinter GUi长时间运行,它会导致错误并没有响应

以下可能会有所帮助:

  • 将代码拆分为类和适当的结构
  • 防止多个while循环和def函数同时运行
  • 不要同时从另一个函数调用多个函数
  • 防止来回循环

但这并不能解决反应迟钝的问题

tkinter是制作小型程序/游戏的基本GUI。即使您的代码没有那么复杂,也最好使用另一种功能强大的GUI

相关问题 更多 >

    热门问题