提前感谢任何帮助。
我有2个.py文件,一个运行GUI,另一个执行基本计算,并使用GUI输入创建图形,然后输出到控制台。这些值打印两次,一次从计算文件打印,再从GUI文件打印。
问题在于,如果我更新输入,则输出不会更新,图表也不会更新。提前感谢您对此的任何帮助。
GUI(test1GUI.py):
import tkinter as tk
from tkinter import ttk
import csv
def To_Print(self):
if len(self.e_xf.get()) != 0:
Write_Inputs_To_CSV(self)
import test1Eqm
test1Eqm.Pre_Output()
Rmin = test1Eqm.Outputs('Rmin')
xf = float(self.e_xf.get())/100
print('xf (fromGUI) = ', xf)
print('Rmin (fromGUI) = ', Rmin)
del test1Eqm
else:
print('Enter All Inputs')
def Write_Inputs_To_CSV(self):
xf = float(self.e_xf.get())/100
with open('TestInputs.csv', 'w', newline='') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
filewriter.writerow(['Variable', 'Value'])
filewriter.writerow(['xf', xf])
class ChemEngApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "ChemEng")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
##### Making Labels #####
self.l_Title = ttk.Label(self, text="Start Page")
self.l_Title.grid(row=0, columnspan = 2)
self.l_xf = ttk.Label(self, text="xA of Feed (%)")
self.l_xf.grid(row=1, sticky="e", padx=5)
##### Making Entry Boxes #####
self.e_xf = ttk.Entry(self)
self.e_xf.grid(row=1, column=1)
self.b_Run = ttk.Button(self, text="Click to Run", command=lambda: RUN(self))
self.b_Run.grid(row=2, column=0, columnspan = 2)
def RUN(self):
To_Print(self)
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
if __name__ == '__main__':
app = ChemEngApp()
app.mainloop()
计算(test1Eqm.py):
import matplotlib.pyplot as plt
import numpy as np
#--------------------------------------------------------------------------------------------------------------------------------
# Getting variables from .csv
import csv
Variables = []
Values = []
def getval(x):
with open('TestInputs.csv', 'rt') as g:
reader = csv.reader(g)
# read file row by row
rowNr = 0
for row in reader:
# Skip the header row.
if rowNr >= 1:
#g.seek(0) <-- makes it freeze on start up
Variables.append(row[0])
Values.append(row[1])
# Increase the row number
rowNr = rowNr + 1
if x == 'xf':
x = float(Values[0])
return x
xf = getval('xf')
Rmin = xf*2.5 # Multiplies xf by 2.5
xl = np.linspace(0,1,2)
yl = [xf, Rmin]
def GUI_MT():
plt.plot(xl, yl, label='Test')
plt.xlabel('xA')
plt.ylabel('yA')
plt.xlim([0, 1])
plt.ylim([0, Rmin*1.5])
plt.legend()
plt.savefig('TestMT.png')
plt.show()
def Pre_Output():
print(' ')
print('New Run')
GUI_MT()
print('xf (fromEqm) = ', xf)
print('Rmin (fromEqm) = ', Rmin)
def Outputs(x):
a = Rmin
if x == 'Rmin':
x = a
return x
if __name__ == '__main__':
print('Run GUI instead')
print('xf = ', xf)
print('Rmin = ', Rmin)
答案 0 :(得分:2)
test1Eqm.py仅在最初导入时读取CSV文件。再次导入它不会导致执行任何代码,您只需获得对现有模块的另一个引用。基本上,当你想要执行计算时,它的所有代码都需要在你实际调用的函数内部。
此外,使用CSV文件在单个程序中将数据从一个模块传输到另一个模块是完全疯狂的。将数据作为参数传递!