r/pythonhelp • u/[deleted] • May 15 '20
Pickling floating point numbers with Tkinter GUI. Please help!
Hi Guys, hope you are all healthy and busy on your python projects during this crisis.
I'm programming my first GUI in Tkinter and I have a small program which takes various user input of pipe sizes/ materials etc (through entry widgets) and outputs weights and fluid weights etc.
I'm a real beginner who learns by banging rocks (or code) together so please don't laugh too much! my code is pretty messy.
I'm just working on the Save / load module now and I'm having some problems getting the pickle module to work with float numbers. My GUI has an output area where results are displayed - these are shown by labels and the get() method to show the results to 2 decimal places by using 'round'.
I want to add persistence to my program and I have used Pickle to get all of these pre-existing entry boxe and result label variables and output them to a .spool file (or could be a .dat) and then load them back in as the user desires. This basically works perfectly apart from the results always come back in as integer numbers and not floats, so basically the result labels lose accuracy in this way. I can't understand why the labels in my results frame are 'getting' the variables and showing them as floats, but the pickle module doesn't with the same variables. I'm sure its something obvious. (I've tried
This is the code for the save/load on my program, any pointers would be graciously received (can you also advise on whether is is okay to have a seperate line for each part of the pickle or if it would be better done as a list?).
Stay safe, cheers.
#Main Save Module
def save():
filename = filedialog.asksaveasfilename(defaultextension='.spool', initialdir = "/",title = "Select file",filetypes = ((".spool files","*.spool"),("all files","*.*")))
#filename = filedialog.asksaveasfilename(defaultextension='.spool', initialdir = "/",title = "Select file",filetypes = ((".dat files","*.dat"),("all files","*.*")))
with open(filename, 'wb') as pickle_file:
#input dumps
pickle.dump(OD.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(WT.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(Wetwt.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(Length.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(extralength1.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(extralength2.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(extralength3.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(extralength4.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(extralength5.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(extralength6.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(extralength7.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(extralength8.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(extralength9.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(extralength10.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(Materialreadout.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
#result dumps
pickle.dump(Area.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(Area1.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(Finalarea.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(weightest.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(Wetwt1.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
pickle.dump(Pipevolume.get(), pickle_file, pickle.HIGHEST_PROTOCOL)
def load():
filename = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = ((".spool files","*.spool"),("all files","*.*")))
#filename = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = ((".dat files","*.dat"),("all files","*.*")))
with open(filename, 'rb') as pickle_file:
text = pickle.load(pickle_file)
OD.set(text),
text1 = pickle.load(pickle_file)
WT.set(text1),
text2 = pickle.load(pickle_file)
Wetwt.set(text2),
text3 = pickle.load(pickle_file)
Length.set(text3),
text4 = pickle.load(pickle_file)
extralength1.set(text4),
text5 = pickle.load(pickle_file)
extralength2.set(text5),
text6 = pickle.load(pickle_file)
extralength3.set(text6),
text7 = pickle.load(pickle_file)
extralength4.set(text7),
text8 = pickle.load(pickle_file)
extralength5.set(text8),
text9 = pickle.load(pickle_file)
extralength6.set(text9),
text10 = pickle.load(pickle_file)
extralength7.set(text10),
text11 = pickle.load(pickle_file)
extralength8.set(text11),
text12 = pickle.load(pickle_file)
extralength9.set(text12),
text13 = pickle.load(pickle_file)
extralength10.set(text13),
text14 = pickle.load(pickle_file)
Materialreadout.set(text14),
command = calculate
#result loads
text15 = pickle.load(pickle_file)
Area.set(text15),
text16 = pickle.load(pickle_file)
Area1.set(text16),
text17 = pickle.load(pickle_file)
Finalarea.set(text17),
text18 = pickle.load(pickle_file)
weightest.set(text18),
text19 = pickle.load(pickle_file)
Wetwt1.set(text19),
text20 = pickle.load(pickle_file)
Pipevolume.set(text20),
1
u/socal_nerdtastic May 15 '20
Make a github repo for your code, or use pastebin, so that we can see the entire code. We can't help from just this snippet.