我想创建一个带有一个用户输入的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
最佳答案
您可以使用 python tkinter 创建 GUI,您还可以使用此库创建输入字段并接受输入的值。之后,您可以简单地使用 python csv 库将记录插入到工作表中。
您可以找到有关 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()
注意:有多种方法可以实现这一目标,其中之一就是这个。
关于python - 如何使用 python 创建 GUI 来获取用户输入并将其插入到 Excel 工作表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59064034/