Reputation: 11
Im having some trouble generating the outputs for a tkinter app that i want to use to allow the user to enter addresses and append them on to an existing csv. file The issue that im having currently is during the export each character in the address is getting delimited so instead of 31A Kabul st, it would be ,3,1,A, ,K,a,b,u,l, ,s,t... I know ive had this problem before and it is probably a simple fix but im new to tkinter and fairly new to python. Any advice would be much appreciated. Cody
from Tkinter import *
from tkFileDialog import *
import csv
"""GUI APPLICATION with Buttons"""
class Application(Frame):
def __init__(self, master):
"""Initialise the frame"""
Frame.__init__(self,master)
self.grid()
self.create_Main()
self.location_columns()
self.Column_box()
self.loader_button()
def create_Main(self):
"""Create 3 buttons that do nothing"""
self.button = Button(self, text="QUIT", fg="red", command=self.quit )
self.button.grid(row =1, column= 6,sticky = E)
self.label0 = Label(self, text= "Enter address")
self.label0.grid(row =1, column= 0, columnspan = 2, sticky = W)
def location_columns(self):
"""Location Columns"""
self.label = Label(self, text= "Flat letter or number")
self.label.grid(row =2, column= 0, sticky = W)
self.label1 = Label(self, text= "Street Number")
self.label1.grid(row =2, column= 1, sticky = W)
self.label2 = Label(self, text= "Street Name")
self.label2.grid(row =2, column= 2, sticky = W)
self.label3 = Label(self, text= "St suffix")
self.label3.grid(row =2, column= 3, sticky = W)
self.label4 = Label(self, text= "Suburb or Town")
self.label4.grid(row =2, column= 4, sticky = W)
self.label5 = Label(self, text= "City")
self.label5.grid(row =2, column= 5, sticky = W)
self.label6 = Label(self, text= "Region")
self.label6.grid(row =2, column= 6, sticky = W)
def Column_box(self):
"""Column Assoc Textboxes"""
self.textbox = Entry(self)
self.textbox.grid(row = 3, column = 0, sticky = W)
self.textbox1 = Entry(self)
self.textbox1.grid(row = 3, column = 1, sticky = W)
self.textbox2 = Entry(self)
self.textbox2.grid(row = 3, column = 2, sticky = W)
self.textbox3 = Entry(self)
self.textbox3.grid(row = 3, column = 3, sticky = W)
self.textbox4 = Entry(self)
self.textbox4.grid(row = 3, column = 4, sticky = W)
self.textbox5 = Entry(self)
self.textbox5.grid(row = 3, column = 5, sticky = W)
self.textbox6 = Entry(self)
self.textbox6.grid(row = 3, column = 6, sticky = W)
self.txt = Text(self, height=4, wrap = NONE)
self.txt.grid(row=5, columnspan=7, sticky="nsew")
self.txt.delete(0.0, END)
self.txt.configure( state="disabled")
self.submit_button = Button(self, text = "Add to current", command = self.reveal)
self.submit_button.grid(row = 4, column = 0, sticky = W)
self.flush_button = Button(self, text = "Add To File", command = self.Address)
self.flush_button.grid(row = 10, column = 0, sticky = W)
"""Scroll Bar"""
scrollb = Scrollbar(self.txt, command=self.txt.yview)
scrollb.pack( side = RIGHT, fill=Y)
self.txt['yscrollcommand'] = scrollb.set
def reveal(self):
"""prevents user from entering/editing the txt box and wrong format"""
self.txt.configure(state="normal")
content = self.textbox.get()
content1 = self.textbox1.get()
content2 = self.textbox2.get()
content3 = self.textbox3.get()
content4 = self.textbox4.get()
content5 = self.textbox5.get()
content6 = self.textbox6.get()
content = str(content.replace(" ",""))
content1 = str(content1.replace(" ", ""))
"""content2 = str(content2.replace(" ", ""))
#dont want to remove the space from roads like
# West Coast Road. etc..."""
content3 = str(content3.replace(" ", ""))
content4 = str(content4.replace(" ", ""))
content5 = str(content5.replace(" ", ""))
content6 = str(content6.replace(" ", ""))
"""Concatenate Flat and address fields"""
try:
content = int(content)
AddNum = str(content) +"/" +str(content1)
except ValueError:
AddNum = str(content1) + str.upper(content)
"""Add all values onto string"""
try:
count = len(AddNum) +len(content2) + len(content3) +len(content4) +len(content5) +len(content6)
if count == 0:
self.errorbox = Label(self, fg="red", text= "Please Enter an Address!")
self.errorbox.grid(row =1, column= 1, columnspan = 2, sticky = W)
raise Exception
else:
try:
self.errorbox.destroy()
message = "'"+ str(AddNum)+"'", "'"+ str(content2) +"'","'"+ str(content3)+"'" , "'"+str(content4) +"'", "'"+str(content5)+"'" , "'"+str(content6)+"'"
except:
message = "'"+ str(AddNum)+"'", "'"+ str(content2) +"'","'"+ str(content3)+"'" , "'"+str(content4) +"'", "'"+str(content5)+"'" , "'"+str(content6)+"'"
except ValueError:
try:
self.errorbox.destroy()
except:
raise Exception
try:
content1 = int(content1)
except ValueError:
self.errorbox = Label(self,fg="red", text= "Street Number must be an integer!")
self.errorbox.grid(row =1, column= 1, columnspan = 2, sticky = W)
raise Exception
"""Prevent interaction with txt box"""
self.txt.insert(0.0 ,'\n')
self.txt.insert(0.0 ,message)
self.txt.grid(row=5, columnspan = 7, sticky="nsew")
self.txt.configure( state="disabled")
def loader_button(self):
self.b_loader = Button(self, text = "Load File", command = self.loader)
self.b_loader.grid(row = 9, column = 0, sticky = W)
def loader(self):
self.filename = askopenfilename(parent=root)
self.textbox10 = Entry(self, width = 60)
self.textbox10.grid(row = 9, column = 1, columnspan = 3, sticky = W)
self.textbox10.insert(0,self.filename)
with open(self.filename, "r") as infile:
Address = infile.read()
self.contbox = Text(self, height=4, wrap = NONE)
self.contbox.grid(row=11, columnspan=7, sticky="nsew")
self.contbox.insert(0.0 ,Address)
self.contbox.configure( state="disabled")
scrolla = Scrollbar(self.contbox, command = self.contbox.yview)
scrolla.pack( side = RIGHT, fill=Y)
self.contbox['yscrollcommand'] = scrolla.set
def Address(self):
Address = self.txt.get(0.0, END)
list1= (Address)
print list1
if len(Address) <= 1:
raise Exception
else:
with open(self.filename, 'a') as csvfile:
listwriter = csv.writer(csvfile, delimiter=',')
print Address
listwriter.writerow(Address)
csvfile.close()
root = Tk()
root.title("AddressApp")
root.geometry("870x300")
app = Application(root)
root.mainloop()
root.destroy()
Upvotes: 1
Views: 799
Reputation: 366083
The issue that im having currently is during the export each character in the address is getting delimited so instead of 31A Kabul st, it would be ,3,1,A, ,K,a,b,u,l, ,s,t…
That almost always means that you're passing a single string, rather than a list of strings, to writerow
. Remember, a string acts like a sequence of one-character strings.
So most likely, what you want to change is:
listwriter.writerow([Address])
However, if you've only got a single column, I'm not sure why you're using CSV in the first place.
More likely, what you actually wanted to do was to make Address
a list in the first place. All those places in your code where you do things like this:
message = "'"+ str(AddNum)+"'", "'"+ str(content2) +"'","'"+ str(content3)+"'" , "'"+str(content4) +"'", "'"+str(content5)+"'" , "'"+str(content6)+"'"
… you probably actually just wanted this:
message = [AddNum, content2, content3, content4, content5, content6]
… at least if message
is the row you're going to eventually try to write to the CSV.
Upvotes: 2