Destroy function not destroying a frame efficiently after the first iteration in Tkinter Python

2024/10/7 12:25:31

I have built a code that saves the calculated data at every iteration in a for loop and the results are stored in 3 different csv files. These saved results are read in another python code that displays the results using GUI tkinter on a window containing three different frames. These three frames are updated every 3 seconds perfectly.

I added a "close" button on each of the three frames that when pressed should DESTROY/CLOSE the specific frame only. At the first iteration and when I press the close button on any of the three frames, the destruction / closure of the frame is done perfectly. However, when time moves on and when more than 1 iteration is done, the close button doesn't close immediately.

For instance, say that 3 iterations and calculations have been done, so when I press the close button it will close on the FOURTH press and before that when it is pressed the frames will show the PREVIOUS results that were shown in the previous iteration .. How can that be edited ? I have attached my code below.


import random
from tkinter import*
import tkinter as tk
import csv
import numpy as np
import time
base = tk.Tk()
base.geometry("500x300")frame1 = tk.LabelFrame(base, text="MVs ", width=240, height=330, bd=15)
frame2 = tk.LabelFrame(base, text="CVs ", width=240, height=330, bd=15)
frame3 = tk.LabelFrame(base, text="DVs ", width=240, height=330, bd=15)def MVs():#read files from saved excel sheetsMV=np.empty(8)with open("MVs.csv","r") as csv_file:count=0csv_reader=csv.reader(csv_file)for row in csv_reader:i=row[0]#change from string to float typeMV[count]=i #update y arraycount +=1 # increment of 1 to get the number of total valuesMV=MV.reshape(8,1)frame1 = tk.LabelFrame(base, text="MVs", width=240, height=330, bd=15)frame1.grid(row=1, column=0,  padx=8)#Display row titlesdef exit_btn():frame1.destroy()#frame1.update()btn1_c=Button(frame1,text="Close",command=exit_btn).grid(column=1,columnspan=1,row=10,rowspan=1, sticky=W)    Label(frame1,text="Tc Measured").grid(column=0,columnspan=1,row=2,rowspan=1, sticky=W)Label(frame1,text="Tc Optimum").grid(column=0,columnspan=1,row=3,rowspan=1, sticky=W)Label(frame1,text="Tc Cost").grid(column=0,columnspan=1,row=4,rowspan=1, sticky=W)Label(frame1,text="Tc Weight").grid(column=0,columnspan=1,row=5,rowspan=1, sticky=W)Label(frame1,text="Q Measured").grid(column=0,columnspan=1,row=6,rowspan=1, sticky=W)Label(frame1,text="Q Optimum").grid(column=0,columnspan=1,row=7,rowspan=1, sticky=W)Label(frame1,text="Q Cost").grid(column=0,columnspan=1,row=8,rowspan=1, sticky=W)Label(frame1,text="Q Weight").grid(column=0,columnspan=1,row=9,rowspan=1, sticky=W)for i in range(8):Label(frame1,text= str(round(MV[i,0],5))).grid(column=1 ,columnspan=1,row=i+2,rowspan=1, sticky=W)def CVs():CV=np.empty(8)with open("CVs.csv","r") as csv_file:count=0csv_reader=csv.reader(csv_file)for row in csv_reader:i=row[0]#change from string to float typeCV[count]=i #update y arraycount +=1 # increment of 1 to get the number of total valuesCV=CV.reshape(8,1)frame2 = tk.LabelFrame(base, text="CVs", width=240, height=330, bd=15)frame2.grid(row=1, column=3,  padx=8)def exit_btn():frame2.destroy()# frame2.update()#Display row titlesbtn2_c=Button(frame2,text="Close",command=exit_btn).grid(column=3,columnspan=1,row=10,rowspan=1, sticky=W)    Label(frame2,text="T Measured").grid(column=3,columnspan=1,row=2,rowspan=1, sticky=W)Label(frame2,text="T Optimum").grid(column=3,columnspan=1,row=3,rowspan=1, sticky=W)Label(frame2,text="T Revenue").grid(column=3,columnspan=1,row=4,rowspan=1, sticky=W)Label(frame2,text="T Weight").grid(column=3,columnspan=1,row=5,rowspan=1, sticky=W)Label(frame2,text="Ca Measured").grid(column=3,columnspan=1,row=6,rowspan=1, sticky=W)Label(frame2,text="Ca Optimum").grid(column=3,columnspan=1,row=7,rowspan=1, sticky=W)Label(frame2,text="Ca Revenue").grid(column=3,columnspan=1,row=8,rowspan=1, sticky=W)Label(frame2,text="Ca Weight").grid(column=3,columnspan=1,row=9,rowspan=1, sticky=W)for i in range(8):Label(frame2,text= str(round(CV[i,0],5))).grid(column=4 ,columnspan=1,row=i+2,rowspan=1, sticky=W)def DVs():dist=np.empty(1)with open("disturbance.csv","r") as csv_file:count=0csv_reader=csv.reader(csv_file)for row in csv_reader:i=row[0]#change from string to float typedist[count]=i #update y arraycount +=1 # increment of 1 to get the number of total valuesdist=dist.reshape(1,1)frame3 = tk.LabelFrame(base, text="DVs ", width=240, height=330, bd=15)frame3.grid(row=1, column=5,  padx=8)def exit_btn():frame3.destroy()# frame3.update()btn3_c=Button(frame3,text="Close",command=exit_btn).grid(column=5,columnspan=1,row=3,rowspan=1, sticky=W)    Label(frame3,text="Q Loss").grid(column=5,columnspan=1,row=2,rowspan=1, sticky=W)Label(frame3,text= str(round(dist[0,0],3))).grid(column=6 ,columnspan=1,row=2,rowspan=1, sticky=W)btn1=Button(base,text='MVs',command=MVs)
btn2=Button(base,text='CVs',command=CVs)
btn3=Button(base,text='DVs',command=DVs)btn1.grid(row=0,column=0)
btn2.grid(row=0,column=3)
btn3.grid(row=0,column=5)i=0
show=True
#show resultswhile(show==True):#base.after(i, MVs) #in msframe1.after(i, MVs)#frame1.after(i+100,frame1.destroy)#base.after(i, CVs) #in msframe2.after(i, CVs)#frame2.after(i+100,frame2.destroy)#base.after(i, DVs) #in msframe3.after(i, DVs)#frame3.after(i+100,frame3.destroy)base.update()#update displayi=i+3000base.mainloop()
Answer

I found a way that solves this problem. However, a new problem arises ..

Basically, I was defining frame1 inside the MVs function. I removed that and kept it only at the beginning. The same approach was done for frame2 and frame3. This way, when I press the close button, the respective frame is immediately closed. However, now when I have closed frame1 for instance, when I press the MVs button again, the frame1 is NOT opening again. It is giving me an error:

_tkinter.TclError: bad window path name ".!labelframe"

If I don't press the close button, this problem does not happen. Hence, the problem is definitely from destroying and closing the frames.

I have attached my edited code here:

import random
from tkinter import*
import tkinter as tk
import csv
import numpy as np
import time
base = tk.Tk()
base.geometry("500x300")frame1 = tk.LabelFrame(base, text="MVs ", width=240, height=330, bd=15)
frame1.grid(row=1, column=0,  padx=8)frame2 = tk.LabelFrame(base, text="CVs ", width=240, height=330, bd=15)
frame2.grid(row=1, column=3,  padx=8)frame3 = tk.LabelFrame(base, text="DVs ", width=240, height=330, bd=15)
frame3.grid(row=1, column=5,  padx=8)z=Falsedef MVs():#read files from saved excel sheetsglobal zglobal frame1if z==True:frame1 = tk.LabelFrame(base, text="MVs", width=240, height=330, bd=15)frame1.grid(row=1, column=0,  padx=8)MV=np.empty(8)with open("MVs.csv","r") as csv_file:count=0csv_reader=csv.reader(csv_file)for row in csv_reader:i=row[0]#change from string to float typeMV[count]=i #update y arraycount +=1 # increment of 1 to get the number of total valuesMV=MV.reshape(8,1)#Display row titlesdef exit_btn():z=Trueframe1.destroy()frame1.update()if z==False:Label(frame1,text="Tc Measured").grid(column=0,columnspan=1,row=2,rowspan=1, sticky=W)Label(frame1,text="Tc Optimum").grid(column=0,columnspan=1,row=3,rowspan=1, sticky=W)Label(frame1,text="Tc Cost").grid(column=0,columnspan=1,row=4,rowspan=1, sticky=W)Label(frame1,text="Tc Weight").grid(column=0,columnspan=1,row=5,rowspan=1, sticky=W)Label(frame1,text="Q Measured").grid(column=0,columnspan=1,row=6,rowspan=1, sticky=W)Label(frame1,text="Q Optimum").grid(column=0,columnspan=1,row=7,rowspan=1, sticky=W)Label(frame1,text="Q Cost").grid(column=0,columnspan=1,row=8,rowspan=1, sticky=W)Label(frame1,text="Q Weight").grid(column=0,columnspan=1,row=9,rowspan=1, sticky=W)for i in range(8):Label(frame1,text= str(round(MV[i,0],5))).grid(column=1 ,columnspan=1,row=i+2,rowspan=1, sticky=W)btn1_c=Button(frame1,text="Close",command=exit_btn).grid(column=1,columnspan=1,row=10,rowspan=1, sticky=W)def CVs():global zglobal frame2if z==True:frame2 = tk.LabelFrame(base, text="CVs", width=240, height=330, bd=15)frame2.grid(row=1, column=3,  padx=8)CV=np.empty(8)with open("CVs.csv","r") as csv_file:count=0csv_reader=csv.reader(csv_file)for row in csv_reader:i=row[0]#change from string to float typeCV[count]=i #update y arraycount +=1 # increment of 1 to get the number of total valuesCV=CV.reshape(8,1)#frame2 = tk.LabelFrame(base, text="CVs", width=240, height=330, bd=15)#frame2.grid(row=1, column=3,  padx=8)def exit_btn():z=Trueframe2.destroy()frame2.update()#Display row titlesLabel(frame2,text="T Measured").grid(column=3,columnspan=1,row=2,rowspan=1, sticky=W)Label(frame2,text="T Optimum").grid(column=3,columnspan=1,row=3,rowspan=1, sticky=W)Label(frame2,text="T Revenue").grid(column=3,columnspan=1,row=4,rowspan=1, sticky=W)Label(frame2,text="T Weight").grid(column=3,columnspan=1,row=5,rowspan=1, sticky=W)Label(frame2,text="Ca Measured").grid(column=3,columnspan=1,row=6,rowspan=1, sticky=W)Label(frame2,text="Ca Optimum").grid(column=3,columnspan=1,row=7,rowspan=1, sticky=W)Label(frame2,text="Ca Revenue").grid(column=3,columnspan=1,row=8,rowspan=1, sticky=W)Label(frame2,text="Ca Weight").grid(column=3,columnspan=1,row=9,rowspan=1, sticky=W)for i in range(8):Label(frame2,text= str(round(CV[i,0],5))).grid(column=4 ,columnspan=1,row=i+2,rowspan=1, sticky=W)btn2_c=Button(frame2,text="Close",command=exit_btn).grid(column=3,columnspan=1,row=10,rowspan=1, sticky=W)   def DVs():dist=np.empty(1)with open("disturbance.csv","r") as csv_file:count=0csv_reader=csv.reader(csv_file)for row in csv_reader:i=row[0]#change from string to float typedist[count]=i #update y arraycount +=1 # increment of 1 to get the number of total valuesdist=dist.reshape(1,1)#frame3 = tk.LabelFrame(base, text="DVs ", width=240, height=330, bd=15)#frame3.grid(row=1, column=5,  padx=8)def exit_btn():frame3.destroy()frame3.update()btn3_c=Button(frame3,text="Close",command=exit_btn).grid(column=5,columnspan=1,row=3,rowspan=1, sticky=W)    Label(frame3,text="Q Loss").grid(column=5,columnspan=1,row=2,rowspan=1, sticky=W)Label(frame3,text= str(round(dist[0,0],3))).grid(column=6 ,columnspan=1,row=2,rowspan=1, sticky=W)btn1=Button(base,text='MVs',command=MVs)
btn2=Button(base,text='CVs',command=CVs)
btn3=Button(base,text='DVs',command=DVs)btn1.grid(row=0,column=0)
btn2.grid(row=0,column=3)
btn3.grid(row=0,column=5)i=0
show=True
#show resultswhile(show==True):#base.after(i, MVs) #in msframe1.after(i, MVs)#frame1.after(i+100,frame1.destroy)#base.after(i, CVs) #in msframe2.after(i, CVs)#frame2.after(i+100,frame2.destroy)#base.after(i, DVs) #in msframe3.after(i, DVs)#frame3.after(i+100,frame3.destroy)base.update()#update displayi=i+3000base.mainloop()
https://en.xdnf.cn/q/118824.html

Related Q&A

Access columns and rows of numpy.ndarray

I currently struggling with extracting certain columns and rows from a matrix stored as a numpy.ndarray. I have a list in which Ive appended these numpy.ndarrays. This list is stored in a variable name…

How to access instance object in list and display there data? in python

class Bank:def __init__(self, name, balance=0):self.name = nameself.balance = balance# def Display_details(self):# print( self.name),# print(self.balance),#### def Withdraw(self, a):# self.…

Using Class, Methods to define variables

I have a number of chemicals with corresponding data held within a database, how do I go about returning a specific chemical, and its data, via its formula, eg o2.class SourceNotDefinedException(Except…

Python Tkinter: Color changing grid of buttons?

I understand that you can make a button that can do some actions when clicked with Tkinter, but how can I just make a button that turns from one color to another when clicked? Then, from that, how do …

Writing a function that checks prime numbers

def primecheck(num): if num > 1: for i in range(2, num): if (num % i) == 0: return False breakelse: return TrueIm trying to make a function that checks if an input is prime or not. This code does …

Getting error code 1 while installing geopandas with pip

This is the error I get when trying to install geopandas using pip install geopandas. Im using Python 3.7.Collecting geopandasUsing cached https://files.pythonhosted.org/packages/24/11/d77c157c16909bd7…

Find if a sorted array of floats contains numbers in a certain range efficiently

I have a sorted numpy array of floats, and I want to know whether this array contains number in a given range. Note that Im not interested in the positions of the number in the array. I only want to k…

Django Operation error: (2026, SSL connection error: SSL_CTX_set_tmp_dh failed)

I can not start my django server after running a statement through manage.py for generating class diagrams from db. And I always get this error but I dont know how to deal with it. OperationalError: (2…

TypeError with module object is not callable

I have a test folder the structure within the folder__init.py__ aa.py test.pyfor aa.pyclass aa:def __init__(self,max):self.max=maxprint max+1def hello(self):print(max)for test.pyimport aa abc = aa(100)…

How to access the GUI output?

Im developing one test bench which runs multiple tests via python gui and prints the output as below.A Passed B Passed C Passed D Passed E PassedButton from gui should be changed to Passed only when A,…