5

I want to create a python GUI with one user input which will be inserted to an excel sheet whenever the user Enters insert button, and another button called e.g Show words, which will read all the words which are inserted into the excel sheet, any ideas how to do that ?

the excel sheet shoud be like this

enter image description here

and the user interface should be something simple like this

enter image description here

some code that I created for GUI but its for text file not 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


programming freak
  • 859
  • 5
  • 14
  • 34

1 Answers1

3

You can create GUI using python tkinter, you can also create input fields using this library and accept the entered value. After this you can simple use python csv library to insert a record into sheet.

You can find more information about tkinter Here

Use this code to read data from test.txt (use your txt file) file, insert data into file also as you asked it will also check if same data exist. You can view the data by clicking on view data button.

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()

Note: There are multiple ways to achieve this, one of them is this one.

Sarvesh
  • 99
  • 5
  • Im trying the same thing with tkinter but for text file and I dont know how to show the output, and the code is not for excel, so I'm absolutely clue less what to do now – programming freak Nov 27 '19 at 07:56
  • 1
    I have updated the answer to read the data from file and show on window after click, let me know if you need any more help. – Sarvesh Nov 27 '19 at 08:56
  • I wasnt able to combine your code and mine can you help with that please, and one more thing if u can help with I dont want to allow the user to enter same text twice so no duplicated texts is allowed in the text file do you have any clue ? – programming freak Nov 27 '19 at 10:42
  • 1
    Hi, @IbrahimOzaeri as you asked i have updated the answer again, hope this will help to achieve your goal. – Sarvesh Nov 28 '19 at 08:03