【问题标题】:how to create a GUI using python to take user input and insert it into excel sheet如何使用 python 创建 GUI 以获取用户输入并将其插入到 Excel 工作表中
【发布时间】:2019-11-27 06:08:11
【问题描述】:

我想创建一个带有一个用户输入的 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 excel user-interface tkinter xlsxwriter


    【解决方案1】:

    您可以使用 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()
    

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

    【讨论】:

    • 我正在用 tkinter 尝试同样的事情,但对于文本文件,我不知道如何显示输出,而且代码不适用于 excel,所以我现在完全不知道该做什么跨度>
    • 我已经更新了从文件中读取数据并在点击后显示在窗口中的答案,如果您需要更多帮助,请告诉我。
    • 我无法将你的代码和我的代码结合起来,请你帮忙,如果你能帮忙,还有一件事我不想让用户输入相同的文本两次,所以没有重复的文本是在文本文件中允许你有什么线索吗?
    • 嗨,@IbrahimOzaeri 正如你所问,我已经再次更新了答案,希望这将有助于实现你的目标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多