的Python 3.8.0
我正在创建一个简单的GUI,以从文件中读取/写入一组货币值。用户可以通过更新文件来创建新的货币,然后GUI会相应地生成Label and Entry小部件。
然后,用户可以更新货币值,然后程序会将这些值写回到文件中,以备下次使用。
我在项目中有3个文件:main_ar.py
-主循环ar_currency_window.py
-GUI框架的类currency.py
-货币和值列表
该代码在下面列出,并且可以正常运行。将生成该窗口并创建Labels和Entry小部件,并使用相应的值填充输入框。
货币作为字典保存,密钥为3个字母的货币代码。我现在需要做的是将这些值读回到字典中,以便将它们保存到文件中。
我一直在努力寻找一种方法来将Entry文本链接到货币词典,但是以此为基础,我能够提出解决方案。
我现在维护一个单独的字典,该字典使用从0开始的序列作为键(等于Entry小部件的行号),并保留货币代码作为值。然后,我循环遍历,并在原始词典中找到货币,然后可以根据需要比较值。
我确实尝试将原始词典绑定到Entry小部件,但是我无法使它正常工作-它没有产生错误,什么也没做。
因此,我的问题是:是否可以将字典绑定到Entry小部件,还是可以解决这个问题?
main_ar.py
from ar_currency_window import Currency
root = Currency()
root.grid()
root.mainloop()
print("finished")
ar_currency_window.py
import tkinter as tk
from tkinter import ttk
class Currency(tk.Tk):
def close_window(self):
self.destroy()
def save_values(self):
for entry in self.currencyList:
findCurrency = self.currencyList[entry]
oldValue = self.currencies[findCurrency]
newValue = self.entries[entry].get()
if oldValue != newValue:
self.currencies[findCurrency] = newValue
print(self.currencies)
def readCurrencies(self):
myfile = "currency.py"
with open(myfile) as raw_data:
for item in raw_data:
if ':' in item:
key, value = item.split(':', 1)
self.currencies[key] = value.strip()
else:
pass # deal with bad lines of text here
print(self.currencies)
def writeCurrencies(self):
myfile = "currency.py"
with open(myfile, 'w') as f:
for key, value in self.currencies.items():
f.write('%s:%s\n' % (key, value))
def __init__(self):
super().__init__()
# The Currencies and Entries strutures
self.currencies = dict()
self.currencyList = dict()
self.entries=[]
# Set the title and frame properties
self.title("AR Currency Management")
self.frame = ttk.Frame(self, padding=(10,10))
# Gets the requested values of the height and width.
windowWidth = self.winfo_reqwidth()
windowHeight = self.winfo_reqheight()
# Gets both half the screen width/height and window width/height
positionRight = int(self.winfo_screenwidth() / 2 - windowWidth / 2)
positionDown = int(self.winfo_screenheight() / 2 - windowHeight / 2)
# Positions the window in the center of the page.
self.geometry("+{}+{}".format(positionRight, positionDown))
# Read in the currencies from the file
self.readCurrencies()
# Now create the labels and entry boxes in the frame
# We use a list to keep track of the entries, so we can reference the entered values later
r = 0 # row counter
for k in self.currencies:
c_label = ttk.Label(self.frame, text=k)
c_input = ttk.Entry(self.frame, textvariable=self.currencies[k])
c_input.insert(0,self.currencies[k])
c_label.grid(column=0, row=r, sticky="W")
c_input.grid(column=1, row=r, sticky="w")
self.entries.append(c_input)
self.currencyList[r]=k
r += 1
# Button to save the values
btnSave = ttk.Button(self.frame, text="Save Currency Values",command=self.save_values)
btnExit = ttk.Button(self.frame, text="Close",command=self.close_window)
btnSave.grid(column=0,row=r+1,sticky="W")
btnExit.grid(column=0, row=r+2, sticky="W")
self.frame.grid()
currency.py
EUR:1.14
CAD:1.13
POL:2.13
NOK:8.23
USD:0.98
OLK:3.33
PLZ:101.54
非常感谢达伦
好的,所以我通过使用列表并最终弄清楚StringVars解决了我的问题!
因此,以下部分代码已更新:
for k in self.currencies:
c_label = ttk.Label(self.frame, text=k)
c_input = ttk.Entry(self.frame, textvariable=self.currencies[k])
c_input.insert(0,self.currencies[k])
对此:
for k in range(len(self.currencies)):
c_label = ttk.Label(master, text=self.currencies[k]['Code'], width=5)
c_label.grid(column=0, row=r, sticky="W")
self.Rates.append( StringVar() )
self.Rates[k].trace("w", lambda name, index, mode, sv=k:
self.validate_rate_data(name, index, mode, self.Rates[sv]))
t = ttk.Entry(master, width=10,textvariable=self.Rates[k])
t.insert(0, self.currencies[k]['Rate'])
t.grid(column=1, row=r, sticky="w", padx=(10, 10))
函数variable_rate_data的定义如下:
def validate_rate_data(self,name, index, mode, var):
#Scan the Rates and ensure they are numeric
try:
f_number = float(var.get())
except ValueError:
print("ERROR")
费率列表定义为 self.Rates = []
该StringVar
跟踪方法被用于调用validate_rate_date
方法时的值在窗口小部件而改变。get()
然后调用该方法以提取值,然后我可以进行验证。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句