因此,我正在创建一个小的数据处理程序,该程序本质上允许用户输入包含您的地址,城市,姓名,电子邮件等值的表格。按“查看人员数据”将打开另一个Tkinter窗口它显示用户先前输入的所有数据(数据存储在文本文件中,然后从中读取以显示数据)。我已经根据数据是什么(例如地址,城市等)在辅助Tkinter窗口的各列中组织了数据,但是地址的最后一列未对齐,并且该类别下的所有条目都略有抬高,并且与其他数据条目。删除地址条目不会将未对齐问题移到下一列条目,而尝试在地址列保持对齐后尝试添加的另一列条目会表明问题是在地址值上隔离的。我不知道这里出了什么问题! 您可以在其中提交数据的启动器窗口的图像:
我的代码:
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()
答案 0 :(得分:2)
您的问题是每个address
都有一个单词\n
。因此,您的Address
有两行。
在函数viewWindow
中更改for循环:
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