How do I pull the updated information from a tkinter form?
Steve
Gronicus at SGA.Ninja
Sun Aug 30 13:15:15 EDT 2020
# With this program, I can read the information from
# Specifications.txt file and display it on a form.
# The user can then update/modify the fields. This is
# all working fine and beeautifully...
#
# I now need to reverse the process and replace the
# adjusted lines of data back into the Specifications.txt
# file. Fortunately, that code has already been
# written .
# What I cannot seem to do is to pull the adjusted
# information from the form into variables, or a
# list/array, so that can be used for the update to the file.
# Suggestions for what to look up to study this
# through will help.
#
# Thank you
# Steve
# ---------------------------------------------------------------
import tkinter as tk
from tkinter import ttk
import sys
ThisList = ["start"]
#===============================================================
def FillTheList():
x=0
ThisList = []
with open("Specifications.txt", 'r') as infile:
for lineEQN in infile: # loop to find each line in the file for that
dose
if lineEQN[0:1] == "-": # Lines with "-" in space 1 have special
recognition for this form
ListItem = lineEQN[1:6].strip() # Remove any spaces before and
after
ThisList.append(ListItem) #Add the ListItem to ThisList
return(ThisList)
#====================================================================
def EditDataByForm():
window = tk.Tk()
window.title("Python Tkinter Text Box")
window.minsize(700,700) #height, width
#===============================================================
def GetLineByItem(DataType): #get line by item in column 3 - 5
#print("DataType = " + DataType)
DataLetter = DataType[0:1] #Capture item letter ID for file check
if DataLetter.isupper(): # == "Specs":
FileToBeEdited = "Specifications.txt"
else:
FileToBeEdited = "DataSpecifications.txt"
with open(FileToBeEdited, 'r') as infile:
for lineEQN in infile: # loop to find each line in the file for
that dose
if ((lineEQN[1:2]== DataLetter)):
A = lineEQN[1:46] # Whole Line
a = lineEQN[34:46].strip() # Just the Data
# print("A = " + A )
# print("a = " + a)
return(A, a) #Return the line and Data separately
#===============================================================
def ClickSubmit():
window.destroy()
#===============================================================
label = ttk.Label(window, text = "Enter the new readings")
label.grid(column = 1, row = 1)
ThisList = FillTheList()
x = 3 # Allows for two rows at the top of the form used for headers
y = 0 # Scans the datafile to fill the list
New_Specs = []
#SpecsToUpdate = []
for lineItem in range(len(ThisList)):
SpecLine, Spec = GetLineByItem(ThisList[y])
OldSpec = Spec
NewSpec = " "
SVRlabel = ttk.Label(window, text = SpecLine + " "*5)
SVRlabel.grid(column = 1, row = x, sticky=tk.W)
NewSpec = tk.StringVar()
New_Specs.append(NewSpec)
SVRCodeEntered = ttk.Entry(window, width = 15, textvariable =
NewSpec)
SVRCodeEntered.grid(column = 2, row = x, pady = 15, sticky=tk.W)
SVRCodeEntered.insert(0, OldSpec)
x += 1
y += 1
#===============================================================
button = ttk.Button(window, text = "Submit", command = ClickSubmit)
button.grid(column= 2, row = 15)
window.mainloop()
#===============================================================
EditDataByForm()
print ("Done")
# Code needed to pull the updated fields from the form
# as variables or in a list/array. Once I have that, the code to replace
# code in the specifications.txt file has already been written.
# Here is a sample of the Specifications.txt file:
----------------------------------------------------------------------------
---------------------------------------
-E MSN Monitor Serial Number JNGY263-T4464 ##
-
-F TSL TestStrip Lot Number 45001 82990 ##
-G SED Strip Expire Date 2021-05-31 ##
-
-H SSC Sensor Sequence Code 71 ##
-I SCN Sensor Code Number G03 ##
-J SSN Sensor Serial Number 2021-01-31 ##
-K SDE Sensor Date to Expire 2021-01-31 ##
-L SDN Sensor Day Number 12 ##
-M FDS First Date for Sensor Fri Aug 17, 2020 09:34 ##
-
-N IDT Insulin Dose Total 450 ##
----------------------------------------------------------------------------
----------------------------
Footnote:
Some mornings it just isn't worth chewing through the leather straps.
More information about the Python-list
mailing list