如何一次从多个Tkinter GUI窗口写入Excel文件?

问题描述 投票:1回答:1

我是Tkinter的新手。尝试使用下面的方法将用户输入从我的多个tkinter GUI写入Excel文件,但最新的值仍然被覆盖。我无法将用户以前的输入值保留到Excel。在这里请求你的帮助。我已经使用numofvm()来创建多个tkinter窗口,因此会创建GUI,然后用户将通过GUI输入值。

from tkinter import *
import xlwt


# designing window for vmdetails

def Vmdetails():
    global vmdetails_screen
    for i in range(1,N+1):
        t=i
        numb=str(t)
        vmdetails_screen = Toplevel(numofvm_screen)
        vmdetails_screen.title("VM_Details_"+ numb)
        vmdetails_screen.geometry("400x950")

        global cluster_name
        global vm_template
        global clustername_entry



        # Set text variables
        cluster_name = StringVar()
        vm_template = StringVar()


        # Set label for user's instruction
        Label(vmdetails_screen, text="Please enter details below", bg="blue").pack()
        Label(vmdetails_screen, text="").pack()

        Cluster_Name_lable = Label(vmdetails_screen, text="Cluster_Name "+ numb +"* ")
        Cluster_Name_lable.pack()

        # Set textvariables entry

        clustername_entry = Entry(vmdetails_screen, textvariable=cluster_name)
        clustername_entry.pack()

        Button(vmdetails_screen, text="Submit", width=10, height=1, command=check).pack()


def check():
    # Retrieve the value from the entry and store it to a variable
    global var
    if cluster_name.get() == '':
        invalid_value()
    else:
     write_to_xls()

def invalid_value():
    global invalid_value_screen
    invalid_value_screen = Toplevel(vmdetails_screen)
    invalid_value_screen.title("Invalid Entry")
    invalid_value_screen.geometry("250x100")
    Label(invalid_value_screen, text="Enter valid values for all the required fields").pack()
    Button(invalid_value_screen, text="OK", command=delete_invalid_value).pack()


def delete_invalid_value():
    invalid_value_screen.destroy()


# designing window to provide the number of vm's to be created

def numofvm():

    global numofvm_screen
    numofvm_screen = Toplevel(main_screen)
    numofvm_screen.title("VM_NUM")
    numofvm_screen.geometry("300x250")

    global vmnumber
    global vmnumber_entry
    vmnumber = StringVar()

    Label(numofvm_screen, text="Please enter the Number of VM's you wishes to create ", bg="blue").pack()
    Label(numofvm_screen, text="").pack()

    vmnumber_lable = Label(numofvm_screen, text="Number of VM's to be created * ")
    vmnumber_lable.pack()

    vmnumber_entry = Entry(numofvm_screen, textvariable=vmnumber)
    vmnumber_entry.pack()

    Button(numofvm_screen, text="Submit", width=10, height=1, command=screen_duplicate).pack()


def screen_duplicate():
    global N
    N = int(vmnumber.get())
    if N > 0  :
      Vmdetails()

#exporting the user inputs into an excel sheet

def write_to_xls():
    # create new workbook
    wb = xlwt.Workbook()

    for i in range(1,N+1):
        t=i
        numb=str(t)
    # add sheet using given name
        ws = wb.add_sheet("VM_"+numb+"_DETAILS")

    # write text to cell
        ws.write(0, 0, "Cluster_Name")
        ws.write(1, 0,cluster_name.get())


    # save to given file name
    wb.save('my_file.xls')


def main_account_screen():
    global main_screen
    main_screen = Tk()
    main_screen.geometry("300x250")
    main_screen.title("Welcome")
    Label(text="Select Your Choice", bg="blue", width="300", height="2", font=("Calibri", 13)).pack()
    Label(text="").pack()
    Button(text="Enter", height="2", width="30", command=numofvm).pack()
    Label(text="").pack()
    main_screen.mainloop()


main_account_screen()
python excel tkinter xlwt
1个回答
1
投票

正如我在评论中解释的那样,数据写入问题是因为你需要存储StringVars并且相关信息是lists而不是仅通过使用单值变量来覆盖先前的值。

虽然可以在不修复的情况下进行修复,但是使用如此多的全局变量会使代码难以理解,调试和修改 - 这些都是常见的智慧,即尽可能避免使用它们。

这是您的问题中的代码使用的列表:

cluster_nameclustername_entryvm_templatevmdetails_screeninvalid_value_screennumofvm_screenvmnumbervmnumber_entryNmain_screen

由于这是相当多的,我决定也在下面的代码中消除它们,以使设计“更清洁”,除了仅显示如何按照建议将数据存储在lists中。希望这将为您的应用程序的持续开发提供更好的基础。

为了避免使用globals,它们中的大多数已被转换为代表整个应用程序的类的属性 - 这是一个基于this answer的设计,以及tkinter guru @Bryan Oakley的anther tkinter问题。

from tkinter import *
import tkinter.messagebox as tkMessageBox
import os
import xlwt


class MyApp(Tk):
    LABEL_BG = "light blue"

    def __init__(self):
        Tk.__init__(self)
        self.geometry("300x250")
        self.title("Welcome")
        Label(text="Select Your Choice", bg=self.LABEL_BG, width="300", height="2",
              font=("Calibri", 13)).pack()
        Label(text="").pack()
        Button(text="Enter", height="2", width="30", command=self.numofvm).pack()
        Label(text="").pack()

    def numofvm(self):
        """ Designing window to provide the number of VMs to be created. """
        self.numofvm_screen = Toplevel(self)
        self.numofvm_screen.title("VM_NUM")
        self.numofvm_screen.geometry("300x250")

        self.vmnumber = StringVar()

        Label(self.numofvm_screen,
              text="Please enter the Number of VMs you wishes to create",
              bg=self.LABEL_BG).pack()
        Label(self.numofvm_screen, text="").pack()

        vmnumber_lable = Label(self.numofvm_screen,
                               text="Number of VMs to be created:")
        vmnumber_lable.pack()

        self.vmnumber_entry = Entry(self.numofvm_screen, textvariable=self.vmnumber)
        self.vmnumber_entry.pack()

        Button(self.numofvm_screen, text="Submit", width=10, height=1,
               command=self.screen_duplicate).pack()

        self.numofvm_screen.focus_set()

    def screen_duplicate(self):
        try:
            self.N = int(self.vmnumber.get())
        except ValueError:
            self.N = 0

        if self.N > 0:
            self.vm_details()

    def vm_details(self):
        """ Create designing windows for VM details.
        """
        # Preallocate and then create VM detail screens and data.
        self.cluster_names = [None for _ in range(self.N)]
        self.clustername_entries = [None for _ in range(self.N)]
        self.vm_templates = [None for _ in range(self.N)]
        self.vmdetails_screen = [None for _ in range(self.N)]

        for i in range(self.N):
            numb = str(i+1)
            self.vmdetails_screen[i] = Toplevel()
            self.vmdetails_screen[i].title("VM Details " + numb)
            self.vmdetails_screen[i].geometry("400x950")

            # Set text variables
            self.cluster_names[i] = StringVar()
            self.vm_templates[i] = StringVar()

            # Set label for user's instruction
            Label(self.vmdetails_screen[i], text="Please enter details below:",
                  bg=self.LABEL_BG).pack()
            Label(self.vmdetails_screen[i], text="").pack()

            Cluster_Name_lable = Label(self.vmdetails_screen[i],
                                       text="Cluster Name "+ numb + ":")
            Cluster_Name_lable.pack()

            # Set textvariables entry
            self.clustername_entries[i] = Entry(self.vmdetails_screen[i],
                                                textvariable=self.cluster_names[i])
            self.clustername_entries[i].pack()

            Button(self.vmdetails_screen[i], text="Submit", width=10, height=1,
                   command=self.validate).pack()

    def validate(self):
        """ Check values of ALL cluster name entries and save them to excel
            file if they're all valid.
        """
        if not all(cluster_name.get() for cluster_name in self.cluster_names):
            self.invalid_value()
        else:
            self.write_to_xls()
            tkMessageBox.showinfo("Info", '"%s" file written' % self.xls_filepath)

            # Get rid of all data entry screens.
            for i in range(self.N):
                self.vmdetails_screen[i].destroy()

            self.numofvm_screen.destroy()

    def invalid_value(self):
        """ Display error message screen.
        """
        self.invalid_value_screen = Toplevel()
        self.invalid_value_screen.title("Invalid Entry")
        self.invalid_value_screen.geometry("400x100")
        Label(self.invalid_value_screen,
              text="Please enter valid values for the fields in ALL\n"
                   "VM Detail windows before clicking Submit button").pack()
        Button(self.invalid_value_screen, text="OK",
               command=lambda: self.invalid_value_screen.destroy()).pack()

    def write_to_xls(self):
        """ Export all user's inputs to an excel sheet. """

        # Create new workbook.
        wb = xlwt.Workbook()

        for i in range(self.N):
            numb = str(i+1)
            # Add sheet using given name.
            ws = wb.add_sheet("VM_" + numb + "_DETAILS")

            # Write text to cell.
            ws.write(0, 0, "Cluster_Name")
            ws.write(1, 0, self.cluster_names[i].get())

        # Save excel file in same directory as script.
        self.xls_filepath = os.path.join(os.path.dirname(__file__), 'my_file.xls')
        wb.save(self.xls_filepath)


if __name__ == '__main__':
    root = MyApp()
    root.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.