在Tkinter中从用户导出到csv文件输入

时间:2016-10-09 14:28:12

标签: python tkinter

我有代码从Tkinter获取用户导入并放入csv文件。在Tkinter窗口中,我可以使用pack来设置输入框,但不喜欢它,而是想使用网格。以下代码适用于包但我无法弄清楚如何做网格。对不起,我有一个月的学习语言。这是代码

from Tkinter import * 
import csv

class App(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.output()
    def output(self):
        Label(text='First Name:').pack    (side=LEFT,padx=5,pady=5)
        self.e = Entry(root, width=10)
        self.e.pack(side=LEFT,padx=5,pady=5)
        Label(text='Last Name:').pack    (side=LEFT,padx=5,pady=5)
        self.e1 = Entry(root, width=10)
        self.e1.pack(side=LEFT,padx=5,pady=5)
        self.b = Button(root, text='Submit',command=self.writeToFile)
        self.b.pack(side=LEFT,padx=5,pady=5)
    def writeToFile(self):
        with open('WorkOrderLog.csv', 'a') as f:
            w=csv.writer(f, delimiter=',')
            w.writerow([self.e.get()])
            w.writerow([self.e1.get()])

if __name__ == "__main__":
    root=Tk()
    root.title('Auto Logger')
    root.geometry('500x200')
    app=App(master=root)
    app.mainloop()
    root.mainloop()

1 个答案:

答案 0 :(得分:0)

使用网格尝试思考就像使用一个表,你将tkinterObjects设置在一个“表”中,然后将行/列设置为这样的东西。

class App(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.output()
    def output(self):
        firstName=Label(self,text='First Name:')
        firstName.grid(row=1,column=0, padx=5, pady=3)
        self.e = Entry(self, width=10)
        self.e.grid(row=1,column=1, padx=5, pady=3)
        lastName=Label(self,text='Last Name:')
        lastName.grid(row=1,column=2, padx=5, pady=3)
        self.e1 = Entry(self, width=10)
        self.e1.grid(row=1,column=3, padx=5, pady=3)
        self.b = Button(self, text='Submit',command=self.writeToFile)
        self.b.grid(row=1,column=4, padx=5, pady=3)
    def writeToFile(self):
        with open('WorkOrderLog.csv', 'a') as f:
            w=csv.writer(f, delimiter=',')
            w.writerow([self.e.get()])
            w.writerow([self.e1.get()])


if __name__ == "__main__":
     root=Tk()
     root.title('Auto Logger')
     root.geometry('500x200')
     app=App(master=root)
     app.mainloop()
     root.mainloop()

所有都在“表”根