如何使用python创建GUI以获取用户输入并将其插入到excel表中

pro*_*eak 5 python excel user-interface tkinter xlsxwriter

我想创建一个带有一个用户输入的 python GUI,每当用户输入插入按钮时,该输入将被插入到 Excel 工作表中,另一个名为例如显示单词的按钮将读取插入到 Excel 工作表中的所有单词,任何想法怎么做 ?

excel表应该是这样的

在此处输入图片说明

用户界面应该像这样简单

在此处输入图片说明

我为 GUI 创建的一些代码,但它的文本文件不是 excel

from tkinter import *

root = Tk()
root.geometry("700x700")
ivn = StringVar()
inputVarName = Entry(root, textvariable=str(ivn))
ivn.set(str("text1"))
inputVarName.grid(row=0, column=0)

ivn2 = StringVar()
inputVarName2 = Entry(root, textvariable=str(ivn2))
ivn2.set(str("text2"))
inputVarName2.grid(row=1, column=0)




def writetofile():
   content_list = [ivn.get(), ivn2.get()]

   print("\n".join(content_list))    
   with open("help.txt", "a") as f:
       for item in content_list:
           f.write("%s\n" % item)

applyButton = Button(root, text="Apply", command=writetofile)
applyButton.grid(row=2, column=1)



root.mainloop() ```
sorry if its silly question but this will be my first python GUI program


Run Code Online (Sandbox Code Playgroud)

Sar*_*esh 3

您可以使用 python tkinter 创建 GUI,还可以使用此库创建输入字段并接受输入的值。之后,您可以简单地使用 python csv 库将记录插入到工作表中。

您可以在这里找到有关 tkinter 的更多信息

使用此代码从 test.txt (使用您的 txt 文件)文件读取数据,也按照您的要求将数据插入文件中,它还会检查是否存在相同的数据。您可以通过单击查看数据按钮来查看数据。

from tkinter import *

root = Tk()
root.geometry("700x700")
ivn = StringVar()
inputVarName = Entry(root, textvariable=str(ivn))
ivn.set(str("text1"))
inputVarName.grid(row=0, column=0)

ivn2 = StringVar()
inputVarName2 = Entry(root, textvariable=str(ivn2))
ivn2.set(str("text2"))
inputVarName2.grid(row=1, column=0)


def printSomething():
    with open('help.txt') as f:
        r = f.read()
    label = Label(root, text=r)
    label.grid()


def checkdata():
    with open('help.txt') as f:
        r = f.read()
    return r.split("\n")


def writetofile():
    exist_data = checkdata()
    content_list = [ivn.get(), ivn2.get()]
    with open("help.txt", "a") as f:
        for item in content_list:
        if item in exist_data:
            msg = "Already exist "+item
            label = Label(root, text=msg)
            label.grid()
        elif not item in exist_data:
            f.write("%s\n" % item)


applyButton = Button(root, text="Add Data", command=writetofile)
applyButton.grid(row=2, column=1)

veiwButton = Button(root, text='View Data', command=printSomething)
veiwButton.grid(row=3, column=1)

root.mainloop()
Run Code Online (Sandbox Code Playgroud)

注意:有多种方法可以实现这一目标,其中之一就是这个。