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

how to create a GUI using python to take user input and insert it into excel sheet

我想创建一个带有一个用户输入的 python GUI,每当用户输入插入按钮时,该输入将被插入到 excel sheet 中,另一个按钮称为例如 Show words ,它将读取插入 excel sheet 中的所有单词,任何想法如何做到这一点?

excelsheet应该是这样的

用户界面应该像这样简单

我为 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


您可以使用 python tkinter 创建 GUI,您也可以使用该库创建输入字段并接受输入的值。在此之后,您可以简单地使用 python csv 库将记录插入 sheet.

您可以找到有关 tkinter 的更多信息Here

使用此代码从 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()

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