Isukali
Isukali

Reputation: 121

Tkinter Label widget displaying out of alignment with other label widgets?

So I am creating a small data handler program that essentially allows the user to turn in a form that takes in values such as your address, city, name, email, etc. Pressing "View Personnel Data" opens up another Tkinter window which displays all the data that users have previously entered in (data which is stored in a text file and then read from to display the data). I have organized the data in columns in a secondary Tkinter window based on what the data is (e.g. address, city, etc) however the final column for addresses is misaligned and all the entries under that category are slightly raised and not in alignment with the other data entries. Removing the address entries does not shift the misalignment issue over to the next column of entries and another column of entries that tried to add after the address column stayed in alignment showing that the issue is isolated on the address values. I can't figure out whats going wrong here! Image of starter windows where you can submit data:

Image of starter windows where you can submit data

Personnel Windows With Address entries misaligned

My code:

import tkinter as t



root = t.Tk()
root.title("Data Handler")
root.geometry('250x250')

#class viewPersons():
    #def open(self):

def addPerson():
    data = open('data.txt', 'a')
    name = name_var.get()
    email = email_var.get()
    age = age_var.get()
    state = state_var.get()
    city = city_var.get()
    address = address_var.get()
    print(name+','+email+','+age+','+state+','+city+','+address)
    data.write(name+','+email+','+age+','+state+','+city+','+address+'\n')
    data.close()

viewopen=False

def viewWindow():
    global viewopen
    def on_close():
        global viewopen
        viewopen = False
        view.destroy()

    if viewopen is False:
        viewopen=True
        view = t.Tk()
        view.geometry('1000x500')

        t.Label(view,text='N͟a͟m͟e͟',borderwidth=2,relief='groove',font=(20)).grid(row=0,column=0)
        t.Label(view, text='E͟m͟a͟i͟l͟',borderwidth=2,relief='groove',font=(20)).grid(row=0, column=1)
        t.Label(view, text='A͟g͟e͟',borderwidth=2,relief='groove',font=(20)).grid(row=0, column=2)
        t.Label(view, text='S͟t͟a͟t͟e͟',borderwidth=2,relief='groove',font=(20)).grid(row=0, column=3)
        t.Label(view, text='C͟i͟t͟y͟',borderwidth=2,relief='groove',font=(20)).grid(row=0, column=4)
        t.Label(view, text='A͟d͟d͟r͟e͟s͟s͟',borderwidth=2,relief='groove',font=(20)).grid(row=0, column=5)
        class Person():
            def __init__(self, name, email, age, state, city, address, rowcounter):
                self.name = name
                self.email = email
                self.age = age
                self.state = state
                self.city = city
                self.address = address
                self.rowcounter = rowcounter

            def display(self):
                t.Label(view, text=self.name,font=(20)).grid(row=self.rowcounter,column=0)
                t.Label(view,text=self.email,font=(20)).grid(row=self.rowcounter,column=1)
                t.Label(view, text=self.age,font=(20)).grid(row=self.rowcounter, column=2)
                t.Label(view, text=self.state,font=(20)).grid(row=self.rowcounter, column=3)
                t.Label(view, text=self.city,font=(20)).grid(row=self.rowcounter, column=4)
                t.Label(view, text=self.address,font=(20)).grid(row=self.rowcounter, column=5)

        data = open('data.txt', 'r+')
        rowcounter=1
        info = data.readlines()
        persons = {}
        person_id = 0
        for x in info:
            split = x.split(',')
            print(split)
            person_id += 1
            persons[person_id] = Person(split[0],split[1],split[2],split[3],split[4],split[5], rowcounter)
            persons[person_id].display()
            rowcounter += 1
        print(persons)
        data.close()
        view.protocol('WM_DELETE_WINDOW', on_close)
        view.mainloop()




name_var = t.StringVar()
email_var = t.StringVar()
age_var = t.StringVar()
state_var = t.StringVar()
city_var = t.StringVar()
address_var = t.StringVar()


t.Label(root, text='Name:').grid(row=0,padx=4)
name_ent = t.Entry(root, textvariable=name_var).grid(row=0,column=1,pady=4)

t.Label(root, text='Email:').grid(row=1,padx=4)
email_ent = t.Entry(root, textvariable=email_var).grid(row=1,column=1,pady=4)

t.Label(root, text='Age:').grid(row=2, padx=4)
age_ent = t.Entry(root, textvariable=age_var).grid(row=2,column=1,pady=4)

t.Label(root, text='State:').grid(row=3, padx=4)
state_ent = t.Entry(root, textvariable=state_var).grid(row=3,column=1,pady=4)

t.Label(root, text='City:').grid(row=4,padx=4)
city_ent = t.Entry(root, textvariable=city_var).grid(row=4,column=1,pady=4)

t.Label(root, text='Address:').grid(row=5,padx=4)
address_ent = t.Entry(root, textvariable=address_var).grid(row=5,column=1,pady=4)

t.Button(root, text='Submit',width=15,command=addPerson).grid(row=6,columnspan=2,ipadx=10)

t.Button(root,text='View Personnel Data',width=15,command=viewWindow).grid(row=7,columnspan=2,ipadx=10)


root.mainloop()

Upvotes: 0

Views: 333

Answers (1)

jizhihaoSAMA
jizhihaoSAMA

Reputation: 12672

Your problem is that each address has a word \n.So your Address has two lines.

Change the for loop in the function viewWindow:

        for x in info:
            split = x.strip().split(',') # remove the `\n`
            print(split)
            person_id += 1
            persons[person_id] = Person(split[0],split[1],split[2],split[3],split[4],split[5], rowcounter)
            persons[person_id].display()
            rowcounter += 1

Upvotes: 2

Related Questions