如何在Excel中保存信息?

时间:2019-12-29 10:36:11

标签: python excel tkinter

我一直在使用不同的教程和书籍(完整的python手册),我的主要目标是将在输入框中输入的信息保存到excel文件中。该程序可以运行,但是没有任何信息保存在excel文件中。 这是我的代码:

   import tkinter as tk
   from openpyxl import *

打开excel文件

     wb = load_workbook(r'C:\Users\Hennie\Downloads\sheet.xlsx')

创建工作表

sheet = wb.active

def a():
    eUSER_field = tk.Entry(root)
    ePASS_field = tk.Entry(root)
    eSQ_field = tk.Entry(root)

    eUSER_field.bind("<Return>", focus1)
    ePASS_field.bind("<Return>", focus2)
    eSQ_field.bind("<Return>", focus3)

def insert():
    # if user not fill any entry
    # then print "empty input"
    global current_row
    if (eUSER_field.get() == "" and
            ePASS_field.get() == "" and
            eSQ_field.get() == ""):

        print("empty input")

    else:
    current_row = sheet.max_row
    current_column = sheet.max_column
    sheet.cell(row=current_row + 1, column=1).value = eUSER_field.get()
    sheet.cell(row=current_row + 1, column=2).value = ePASS_field.get()
    sheet.cell(row=current_row + 1, column=3).value = eSQ_field.get()

保存文件

    wb.save(r'C:\Users\Hennie\Downloads\sheet.xlsx')

将焦点放在“用户名”字段上

     eUSER_field.focus_set()

调用clear()函数

     clear()

        def focus1(eUSER):
            eUSER.focus_set()
        def focus2(ePASS):
            ePASS.focus_set()
        def focus3(eSQ):
            eSQ.focus_set()

        def excel():

调整

中列的宽度

excel电子表格

sheet.column_dimensions['A'].width = 30
sheet.column_dimensions['B'].width = 10
sheet.column_dimensions['C'].width = 10

# write data to the excel spreadsheet
# at particular location



     sheet.cell(row=1, column=1).value = "User Name"
        sheet.cell(row=1, column=2).value = "Password"
        sheet.cell(row=1, column=3).value = "Security question"

        def clear():

清除文本输入框的内容

     eUSER_field.delete(0)
        ePASS_field.delete(0)
        eSQ_field.delete(0)



class BuildApplication(tk.Frame):

    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.pack()
        self.createWidgets()
    def createWidgets(self):
        self.lUSER = tk.Label(self, text="Username: ")
        self.lUSER.grid(row=1, column=2)
        self.eUSER = tk.Entry(self)
        self.eUSER.grid(row=2, column=2)
        self.lPASS = tk.Label(self, text="Password: ")
        self.lPASS.grid(row=3, column=2)
        self.ePASS = tk.Entry(self, show="*")
        self.ePASS.grid(row=4, column=2)
        self.LOGIN = tk.Button(self, text="Create user", fg="green", command=self.setCredentials)
        self.LOGIN.grid(row=5, column=2)

        self.QUIT = tk.Button(self, text="QUIT", fg="red", command=insert)
        self.QUIT.grid(row=6, column=2)

     self.mainloop()

def setCredentials(self):
    username = self.eUSER.get()
    password = self.ePASS.get()
    print("username", username)
    print("password", password)
    self.master.destroy()

    enter code here
    class SearchApplication(tk.Frame):

        def __init__(self, master=None):
            tk.Frame.__init__(self, master)
            self.pack()
            self.createWidgets()

        def createWidgets(self):
            print()
            self.some_abel = tk.Label(self, text="This will be official login")
            self.some_abel.grid(row=1, column=2)
            self.quitb = tk.Button(self, text="quit", fg="green", command=self.master.destroy)
            self.quitb.grid(row=2, column=2)


    class MainApplication(tk.Frame):
        def __init__(self, master=None):
            tk.Frame.__init__(self, master)
            self.grid()
            self.createWidgets()

        def createWidgets(self):

创建新用户

            self.bBuild = tk.Button(self, text="Create new user", command=self.build)
            self.bBuild.grid(row=1, column=2, ipadx=1, ipady=1)

搜索数据

     self.bSearch = tk.Button(self, text="Login", command=self.search)
        self.bSearch["command"] = self.search
        self.bSearch.grid(row=2, column=2, ipadx=1, ipady=1)
        self.QUIT = tk.Button(self, text="QUIT", fg="red", command=self.master.destroy)
        self.QUIT.grid(row=3, column=2, ipadx=1, ipady=1)
        def build(self):
        print("Building")
        root2 = tk.Toplevel()
        buildApp = BuildApplication(master=root2)
        def search(self):
        print("Search")
        root3 = tk.Toplevel()
        app2 = SearchApplication(master=root3)
        if __name__ == "__main__":
        root = tk.Tk()

        excel()

        filename = tk.PhotoImage(file="C:\\Users\\Hennie\\Desktop\\N.A.M.E\\background.png")
        background_label = tk.Label(image=filename)
        background_label.place(x=0, y=0, relwidth=1, relheight=1)
        root.geometry("500x500+0+0")
        app = MainApplication(master=root)
        app.master.title("Experiment")
        app.master.iconbitmap(r'C:\Users\Hennie\Downloads\logo.ico')  
        excel()
        app.mainloop() 

0 个答案:

没有答案