Advice on how to store data - python

I am fairly new to coding and I am making an app in tkinter with a window that takes a series of user inputs(string&int). i need to be able to open this window multiple times and input different data and i need it to save it every time so I can display the information in seperate windows later on. i will include the code for the window. to be clear, i don't want to overwrite the data, i want it to save each set of data seperately. any help (code or advice) would be great :) thanks.
import tkinter as tk
from tkinter import *
add = tk.Tk()
add.title("Add a new Exersise")
add.geometry('600x600')
add.configure(bg='grey')
add.grid_columnconfigure(0, weight=1)
ExNm = tk.Label(add, bg='grey', text="Exercise Name")
ExNm.grid(row=0, column=0, pady=10, sticky='nesw')
Name = tk.Entry(add)
Name.grid(row=1, column=0, pady=10, padx=15, sticky='nesw')
YtLink = tk.Label(add, bg='grey', text="Youtube Link")
YtLink.grid(row=2, column=0, pady=10, sticky='nesw')
Link = tk.Entry(add)
Link.grid(row=3, column=0, pady=10, padx=15, sticky='nesw')
DocLink = tk.Label(add, bg='grey', text="Tab Document | e.g. Alternate Picking.pdf")
DocLink.grid(row=4, column=0, pady=10, sticky='nesw')
Tab = tk.Entry(add)
Tab.grid(row=5, column=0, pady=10, padx=15, sticky='nesw')
def sliderFunction(event):
Beats = BPM.get()
print(Beats)
BPM_Label = tk.Label(add, bg='grey', text="BPM")
BPM_Label.grid(row=6, column=0, pady=10, sticky='nesw')
BPM = Scale(add,from_=0, to=400, orient=HORIZONTAL, font=('Helvetica','15'))
BPM.bind("<ButtonRelease-1>", sliderFunction)
BPM.grid(row=7, column=0, pady=10, padx=15, sticky='nesw')

Here's a link.
Normally you could build a treeview and store then into CSV.

Related

How do I access the string in an entry box if its inside a frame (python)

How do I access the Entry and its contents when its inside a frame? I tried Entry.get() to print it but it doesnt work, I also tried printing the frame itself but it didnt really work, I want to be able to print out what I write inside the entry box, help is appreciated, thank you!
frame8 = Frame(window1, width=500, height=400, bd=1, bg='#474545')
frame8.pack()
iframe9 = Frame(frame, bd=2, relief=RIDGE)
Label(iframe9, text='Fees:').pack(side=LEFT, padx=5)
Entry(iframe9, bg='white', fg='#474545').pack(side=RIGHT, padx=5)
iframe9.pack(expand=1, fill=X, pady=10, padx=5)
def submit():
print(Entry.get())
frame10 = Frame(window1, width=500, height=400, bd=1, bg='#474545')
frame10.pack()
iframe11 = Button(frame, bd=2, relief=RIDGE, text="Submit", command=submit)
t = StringVar()
iframe11.pack(expand=1, fill=X, pady=10, padx=5)
You need to save the Entry object in a variable, just like you did with the frame. Then, you use get on that object.
box = Entry(iframe9, bg='white', fg='#474545')
box.pack(side=RIGHT, padx=5)
...
print(box.get())

How to get paragraph data from Text widget in Tkinter for Python 3.x? How do I take paragraph input from the user and add the data to the CSV file?

Unable to extract data from Text widget in Tkinter, Python. However, it works fine for the Entry widgets that are in the code.
My purpose is to take paragraph input from the user and then add the data to a CSV file and subsequently generate a prescription using Photoshop.
Here is the code:
main.py
import abc
import csv, datetime
from tkinter import *
from tkinter import ttk
now=datetime.datetime.now().strftime("%d-%m-%Y")
def DataAdder2CSV():
global edate, eSNO, eage, egender, ename, ePID, econtact, ecomp, eallergy, ehistory, eR
a=edate.get()
b=eSNO.get()
c=eage.get()
d=egender.get()
e=ename.get()
f=ePID.get()
g=econtact.get()
data=[a,b,c,d,e,f,g,ecomp, eallergy, ehistory, eR]
print(data)
file=open("Patient_Data.csv","a", newline="")
writer=csv.writer(file, delimiter=",")
writer.writerow(data)
file.close()
def PrescGen():
pass
root=Tk()
root.config(bg="#add8e6")
megaF=LabelFrame(root,bg="#add8e6", borderwidth=0)
megaF.pack()
greet=Label(megaF,text="Prescription Maker", bg="#add8e6", fg="black", font="LucidaConsole 30").pack(pady=20)
dateF=Frame(megaF, padx=10, pady=10, bg="#c3dde6")
dateF.pack()
date=Label(dateF,text="Date:", bg="#c3dde6").grid(row=0,column=0)
edate=Entry(dateF, width=10)
edate.insert(0,now)
edate.grid(row=0, column=1)
mainL=Frame(root,borderwidth=0, padx=10, pady=10, bg="#c3dde6")
mainL.pack(pady=20, padx=40)
PDlf=Frame(mainL, padx=10, pady=13, bg="#c3dde6")
PDlf.grid(row=0, column=0, sticky=NW)
sno=Label(PDlf,text="Sno:", bg="#c3dde6").grid(row=0,column=0)
eSNO=Entry(PDlf, width=3)
eSNO.grid(row=0, column=1)
age=Label(PDlf,text="Age:", bg="#c3dde6").grid(row=1,column=0)
eage=Entry(PDlf, width=3)
eage.grid(row=1, column=1, pady=10)
gender=Label(PDlf,text="Gender:", bg="#c3dde6").grid(row=2,column=0)
egender=Entry(PDlf, width=3)
egender.grid(row=2, column=1)
name=Label(PDlf,text="Name:", bg="#c3dde6").grid(row=0,column=3)
ename=Entry(PDlf, width=15)
ename.grid(row=0, column=4)
Pid=Label(PDlf,text="PatientID:", bg="#c3dde6").grid(row=1,column=3)
ePID=Entry(PDlf, width=15)
ePID.grid(row=1, column=4, pady=10)
contact=Label(PDlf,text="Contact:", bg="#c3dde6").grid(row=2,column=3)
econtact=Entry(PDlf, width=15)
econtact.grid(row=2, column=4)
blank=Label(PDlf,text=" ", bg="#c3dde6").grid(row=1,column=2)
contentLF=LabelFrame(mainL, padx=10, pady=10, bg="#c3dde6", borderwidth=0)
contentLF.grid(row=0, column=2, sticky=SE)
#Never gonna give you up, never gonna let you down
blank=Label(PDlf,text=" ", bg="#c3dde6").grid(row=0,column=2)
complaint=Label(contentLF,text="Complaint:", bg="#c3dde6").grid(row=0,column=1)
ecomp=Text(contentLF, width=50, height=3).grid(row=0, column=2)
allergy=Label(contentLF,text="Allergy:", bg="#c3dde6").grid(row=1,column=1)
eallergy=Text(contentLF, width=50, height=2)
eallergy.grid(row=1, column=2, pady=10)
history=Label(contentLF,text="History:", bg="#c3dde6").grid(row=2,column=1)
ehistory=Text(contentLF, width=50, height=5)
ehistory.grid(row=2, column=2)
R=Label(contentLF,text="Diagnosis:", bg="#c3dde6").grid(row=3,column=1)
eR=Text(contentLF, width=50, height=12)
eR.grid(row=3, column=2, pady=10)
buttF=LabelFrame(mainL, bg="#c3dde6", borderwidth=0)
buttF.grid(row=4, column=2, sticky=E)
genPres=Button(buttF,text="Generate Prescription", bg="#ff80b9", command=PrescGen).grid(row=0, column=0, padx=10, sticky=E)
csvAdd=Button(buttF,text="Add to Database", bg="#49ff9a", command=DataAdder2CSV).grid(row=0, column=1,padx=10, sticky=E)
pic=Frame(mainL, padx=10, pady=15, bg="#c3dde6")
pic.grid(row=1, column=0, sticky=NW)
root.title("Prescription Maker")
root.state("zoomed")
root.mainloop()
Here is the output:
Here is the CSV file where I extracted all the data to:
How do I take paragraph input from the user and add the data to the CSV file (Tkinter)?

Using a Tkinter Widget from Lower Down in the Program

So I'm trying to make this tkinter GUI in which you can add or search for a customer (the customer is stored in a SQlite database). Problem is, I can't see a way of making the def for when the add customer button is pressed, for as far as I know, I would need to use the names of the variables from lower down in the program where the widgets are created. I could switch the order of the functions around, but then the widgets would be calling functions that have not already been created. Can someone help fix this probem? Thanks in advance.
Here's the code:
from tkinter import *
app = Tk()
app.minsize(400,420)
app.title("Gatecode Manager")
app.resizable(False, False)
def add():
first = createWindow().firstAdd.get()
last = createWindow().firstAdd.get()
phone = createWindow().numberAdd.get()
def createWindow():
Label(text="Welcome to Gatecode Manager", font=("bold", 17)).grid(row=0, columnspan=2, pady=30, sticky=N)
Label(text="Search for a Customer").grid(row=1, column=0, padx=30, pady=10)
Label(text="First Name:").grid(row=2, column=0, padx=30, pady=10)
firstSearch = Entry().grid(row=3, column=0, padx=30)
Label(text="Last Name:").grid(row=4, column=0, padx=30, pady=10)
lastSearch = Entry().grid(row=5, column=0, padx=30)
Label(text="Add Customer to System").grid(row=1, column=1, padx=30, pady=10)
Label(text="First Name:").grid(row=2, column=1, padx=30, pady=10)
firstAdd = Entry().grid(row=3, column=1, padx=30)
Label(text="Last Name:").grid(row=4, column=1, padx=30, pady=10)
lastAdd = Entry().grid(row=5, column=1, padx=30)
Label(text="Phone Number:").grid(row=6, column=1, padx=30, pady=10)
numberAdd = Entry().grid(row=7, column=1, padx=30)
add = Button(text="Add", command="null").grid(padx=30, pady=10, column=1)
app.mainloop()
createWindow()
I find it difficult to understand exactly how you want this program to function. Is the add button supposed to call the function add() which then calls createWindow()?
I'm guessing a little here but what about:
1) Don't create the GUI in a function. A function will keep the variables in the scope of the functioin.
2) Use StringVar() to access entries. Since you don't save the identity of your entries it becomes impossible to interrogate them.
Example:
from tkinter import *
app = Tk()
app.minsize(400,420)
app.title("Gatecode Manager")
app.resizable(False, False)
def add():
first = firstAddVar.get()
last = firstAddVar.get()
phone = numberAddVar.get()
Label(text="Welcome to Gatecode Manager", font=("bold", 17)).grid(row=0,
columnspan=2, pady=30, sticky=N)
Label(text="Search for a Customer").grid(row=1, column=0, padx=30, pady=10)
Label(text="First Name:").grid(row=2, column=0, padx=30, pady=10)
firstSearchVar = StringVar()
firstSearch = Entry(textvar=firstSearchVar).grid(row=3, column=0, padx=30)
Label(text="Last Name:").grid(row=4, column=0, padx=30, pady=10)
lastSearchVar = StringVar()
lastSearch = Entry(textvar=lastSearchVar).grid(row=5, column=0, padx=30)
Label(text="Add Customer to System").grid(row=1, column=1, padx=30, pady=10)
Label(text="First Name:").grid(row=2, column=1, padx=30, pady=10)
firstAddVar = StringVar()
firstAdd = Entry(textvariable=firstAddVar).grid(row=3, column=1, padx=30)
Label(text="Last Name:").grid(row=4, column=1, padx=30, pady=10)
lastAddVar = StringVar()
lastAdd = Entry(textvariable=lastAddVar).grid(row=5, column=1, padx=30)
Label(text="Phone Number:").grid(row=6, column=1, padx=30, pady=10)
numberAddVar = StringVar()
numberAdd = Entry(textvariable=numberAddVar).grid(row=7, column=1, padx=30)
add = Button(text="Add", command=add).grid(padx=30, pady=10, column=1)
app.mainloop()
When your GUIs become more complex you should study OOP.

How do I make two LabelFrames align with each other?

I have two tk.LabelFrame widgets which should be equal when it comes to width. I have tried to fine-tune each of the widgets (and the widgets inside), but nothing worked out so far. It's either too much, or too little. For instance, 14 is too little, and 15 is too much, and 14.5 is not accepted (of course).
Current code:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
material_label_frame = tk.LabelFrame(root)
material_label_frame.grid(row=0, column=0, sticky=tk.W)
material_label = ttk.Label(material_label_frame, text='Material:')
material_label.grid(row=0, column=0, padx=5, pady=5)
material_combobox = ttk.Combobox(material_label_frame, width=15, values=['Gold', 'Silver'])
material_combobox.grid(row=0, column=1, padx=5, pady=5, sticky=tk.W)
weight_label_frame = tk.LabelFrame(root)
weight_label_frame.grid(row=1, column=0, sticky=tk.W)
weight_label = ttk.Label(weight_label_frame, text='Weight:')
weight_label.grid(row=0, column=0, padx=(5, 14), pady=5)
weight_entry = ttk.Entry(weight_label_frame, width=11)
weight_entry.grid(row=0, column=1)
weight_combobox = ttk.Combobox(weight_label_frame, width=3, values=['g', 'kg', 'oz'])
weight_combobox.grid(row=0, column=2, padx=(0, 5), pady=5, sticky=tk.W)
root.mainloop()
I would really appreciate it if someone helped me overcome the issue.
Use sticky="ew" to get the frame to fill the column. If both frames are in the same column and both use the same sticky value to stick to both sides of the columns, they will align up precisely.
material_label_frame.grid(row=0, column=0, sticky="ew")
weight_label_frame.grid(row=1, column=0, sticky="ew")
Try to use place function instead grid function. Place is more complicated, but let you be more precisely in your design interface.
Like this:
weight_entry = ttk.Entry(weight_label_frame, width=11)
weight_entry.place(x=10, y=75, width = 120)

Python Tkinter grid spacing of widgets and LablelFrames not right

I am designing a simple GUI in Python 2.7 Tkinter, but I can't get things to spread out as I want them. I have managed to get my various widgets roughly where I want them, however I can't seem to force spacing out and things are a little bunched up.
I have also tried to draw 3 LabelFrames to separate the window out, but widgets seem to fall over the LabelFrames. I am wondering how I can space this out a little better. The grid system seems to allow things to bunch up and ignores blank rows and columns as far as I can see.
from Tkinter import *
import Tkinter, Tkconstants, tkFileDialog, tkMessageBox
class FileZap():
def __init__(self, root):
root.title("TestGUI")
root.geometry("860x450")
self.topFrame = LabelFrame(root, text="Top Area")
self.topFrame.grid(row=1, column=1, rowspan=6, columnspan=7, padx=5, pady = 5, sticky="NSEW")
self.listbox1 = Listbox(root, width=50, selectmode="multiple")
self.listbox1.grid(row=3, column=2)
self.scrollbar = Scrollbar(orient=VERTICAL, command=self.listbox1.yview)
self.listbox1.config(yscrollcommand=self.scrollbar.set)
self.scrollbar.grid(row=3, column=3, sticky="ns")
self.listbox2 = Listbox(root, width=50)
self.listbox2.grid(row=3, column=4)
self.selectLabel = Label(root, text="Select a folder: ")
self.selectLabel.grid(row=3, column=1)
self.user1 = Entry(root, width="50")
self.user1.grid(row=2, column=2)
self.browse = Button(root, text="Browse")
self.browse.grid(row=2, column=3)
self.addItems = Button(root, text="Add to Selection")
self.addItems.grid(row=4, column=2)
self.clearItems = Button(root, text="Clear Selection")
self.clearItems.grid(row=4, column=4)
self.leftFrame = LabelFrame(root, text="Left Area")
self.leftFrame.grid(row=5, column=1, rowspan=6, columnspan=3, padx=5, pady = 5, sticky="NSEW")
self.replaceInLable = Label(root, text="String to replace: ")
self.replaceOutLable = Label(root, text="New string: ")
self.replaceInLable.grid(row=7, column=1)
self.replaceOutLable.grid(row=7, column=2)
self.replaceIn = Entry(root, width="20")
self.replaceOut = Entry(root, width="20")
self.replaceIn.grid(row=8, column=1)
self.replaceOut.grid(row=8, column=2)
self.replace = Button(root, text="Replace")
self.replace.grid(row=8,column=3)
self.rightFrame = LabelFrame(root, text="Right Area")
self.rightFrame.grid(row=5, column=4, rowspan=6, columnspan=3, padx=5, pady = 5, sticky="NSEW")
self.quit = Button(root, text="Exit", command=root.quit)
self.quit.grid(row=9, column=6)
root = Tkinter.Tk()
file_zap = FileZap(root)
root.mainloop()
I have tried various alterations but can't nail it! Any help would be much appreciated.
First, the columns / row adapt to there content so an empty one as a zero height/width. If you want to put space between your widgets use the padx and pady options in the .grid method. They can take either one number which will give the padding on both sides or a couple of numbers giving the padding on each side.
Secondly, if you want your widgets to be inside a LabelFrame, you need to create them with this LabelFrame as master instead of the main window.
from Tkinter import LabelFrame, Tk, Button, Label
root = Tk()
# make row 0 resize with the window
root.rowconfigure(0, weight=1)
# make column 0 and 1 resize with the window
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=1)
# create LabelFrames
top_frame = LabelFrame(root, text="top")
left_frame = LabelFrame(root, text="left")
right_frame = LabelFrame(root, text="right")
top_frame.grid(row=0, column=0, columnspan=2, padx=10, pady=(10,4), sticky="nsew")
left_frame.grid(row=1, column=0, padx=(10,4), pady=4, sticky="nsew")
right_frame.grid(row=1, column=1, padx=(4,10), pady=4, sticky="nsew")
#create widgets inside top_frame
Label(top_frame, text="I'm inside top_frame").pack()
Button(top_frame, text="Top").pack()
#create widgets inside left_frame
Label(left_frame, text="I'm inside left_frame").pack()
Button(left_frame, text="Left").pack()
#create widgets inside top_frame
Label(right_frame, text="I'm inside right_frame").pack()
Button(right_frame, text="Right").pack()
Button(root, text="Quit", command=root.destroy).grid(row=2, column=0,
columnspan=2, pady=10)
root.mainloop()

Categories