我已经在Tkinter应用程序上工作了两天,我无法弄清楚如何将信息从一个帧传递到另一个帧。
我试图用字典来做这个
keys = ['key1','key2','key3']
result_dict = dict.fromkeys(keys)
class Class1():
.......
def updateDict(self):
####Do Calculations here
result_dict['key1'] = newvalue
result_dict['key2'] = new_key2_value
result_dict['key3'] new_key3_value
class Class2():
......
print result_dict['key3']
基本上,我在所有类之外初始化一个dict,然后在我的第一个类中更新它,然后尝试在我的第二个类中调用更新的dict。但是,它一直指向第一个带有None类型的Dict。
我一直试图解决这个问题几个小时。非常令人沮丧。我究竟做错了什么?结果是打印None不会引发错误。
编辑:这是此问题的完整代码。
import Tkinter as tk
import ttk
import pandas as pd
import numpy as np
LARGE_FONT = ("Verdana", 12)
keys = ['Month','Year','Site','Operator','Amount','Value','Field']
result_dict = dict.fromkeys(keys)
def combine_funcs(*funcs):
def combined_func(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return combined_func
class Database(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
###Sets the Window title
tk.Tk.wm_title(self,"Database Searcher")
###Container is our Frame
container = tk.Frame(self)
###Sets where this is
container.pack(side="top", fill ="both", expand = True)
###Setting Row Attributes
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight =1)
#Makes a Dictionary for adding more Frames
self.frames = {}
###Main Page Frame
for F in (MainPage,ResultPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky = "nsew")
###Shows the main frame
self.show_frame(MainPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class MainPage(tk.Frame):
def __init__(self, parent, controller):
####Init the Frame
tk.Frame.__init__(self,parent)
self.data_selection =[]
self.operator =[]
self.data_value=[]
self.year_value = []
self.site_value =[]
self.mon_value= []
###Label the Page as Main Page
label = tk.Label(self, text ="Fill out the Information Below", font = LARGE_FONT)
label.pack()
###Create a Month Dropdown Menu
monList = ('1','2','3','4','5','6','7','8','9','10','11','12')
self.mon_field = tk.StringVar()
monMenu = ttk.OptionMenu(self,self.mon_field, 'Month', *monList)
monMenu.pack(side="top")
####Make an Entry for Year Data
self.year_field = tk.StringVar(self, value = 'Enter Year')
yearEntry = ttk.Entry(self, textvariable = self.year_field )
yearEntry.pack()
###Sites
siteList = ('KMCI','KMKC','KCOU','KSTJ','KJEF','KSGF','KSTL')
self.site_field = tk.StringVar()
siteOptionMenu = ttk.OptionMenu(self, self.site_field,'Site',*siteList)
siteOptionMenu.pack()
###Our dataList tuple for the dataList Option Menu
dataList = ('High Temp','Low Temp','Precipitation','Snowfall', 'Snow Depth')
###Need to make a SringVar for the Menu ( this is how the .get() method works
self.data_field = tk.StringVar()
optionMenu2 = ttk.OptionMenu(self, self.data_field ,dataList[0],*dataList)
###Pack it and add it!
optionMenu2.pack(side ='left')
#####Operator Option Menu
operatorList = ('Equal To','Greater Than','Less Than','Greater Than or Equal To',
'Less Than or Equal To')
self.operator_field = tk.StringVar()
optionMenu1 = ttk.OptionMenu(self,self.operator_field,operatorList[0],*operatorList)
optionMenu1.pack(side ='left')
###Make a Entry text box for inputting the Values
self.value_field = tk.StringVar(self, value = 'Enter Value Here')
entry1 = ttk.Entry(self, textvariable = self.value_field)
entry1.pack(side ="left", padx = 5)
###Add a Buttom commanded to databaseCalc Formula that makes
button1 = tk.Button(self, text = 'Get Data!', command = combine_funcs(self.databaseCalc, lambda: controller.show_frame(ResultPage)))
button1.pack()
def databaseCalc(self):
data_selection = self.data_field.get()
operator = self.operator_field.get()
data_value = int(self.value_field.get())
year_value = int(self.year_field.get())
site_value = self.site_field.get()
mon_value = int(self.mon_field.get())
df = pd.read_csv('C:/Users/Michael/Desktop/Daily Data Proc/' + site_value +'.csv')
data = (df[data_selection]).as_matrix()
month = (df['MONTH']).as_matrix()
year = (df['YEAR']).as_matrix()
if operator == 'Equal To':
indc = np.where((data == data_value) & (month == mon_value) & (year == year_value))
temp = data[indc]
self.amount = (np.ones_like(temp)).sum()
elif operator == 'Greater Than':
indc = np.where((data > data_value) & (month == mon_value) & (year == year_value))
temp = data[indc]
self.amount = (np.ones_like(temp)).sum()
elif operator == 'Less Than':
indc = np.where((data < data_value) & (month == mon_value) & (year == year_value))
temp = data[indc]
self.amount = (np.ones_like(temp)).sum()
elif operator == 'Greater Than or Equal To':
indc = np.where((data >= data_value) & (month == mon_value) & (year == year_value))
temp = data[indc]
self.amount = (np.ones_like(temp)).sum()
elif operator == 'Less than or Equal To':
indc = np.where((data <= data_value) & (month == mon_value) & (year == year_value))
temp = data[indc]
self.amount = (np.ones_like(temp)).sum()
## result_dict.update('Month' : self.mon_field, 'Year': self.year_field, 'Site': self.site_field,
## 'Operator': self.operator, 'Amount' = self.amount, 'Value' = self.data_value, 'Field' = self.data_selection)
result_dict['Month'] = mon_value
result_dict['Year'] = year_value
result_dict['Site'] = site_value
result_dict['Operator'] = operator
result_dict['Amount'] = self.amount
result_dict['Value'] = data_value
result_dict['Field'] = data_selection
class ResultPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
###Create a Label with the results that we would like to display
label1 = tk.Label(self, text = str(result_dict['Amount']), font = LARGE_FONT)
label1.pack()
###Create a Button to go Backwards
button2 = tk.Button(self, text = 'Run Another Query', command = lambda: controller.show_frame(MainPage))
button2.pack()
app = Database()
app.mainloop()
在MainPage中的dataCalc()是我更新我在前几行代码中启动的dict的地方。然后,我想在类ResultPage中使用这些更新的值,并在窗口中显示它们。此代码有效,但将None列为ResultPage上的标签