-1

Hi guys i'm trying to make an app which takes data from some tkinter entry boxes adds them to a database and also displays them in a multi list box from tktreectrl. The code I have tried to use to add things to the database was based on various online tutorials however all this is doing is adding a 0 in the last column of the database. And even if I put values into the database using DB Browser they are still not displayed in the multi list box. (Here is the full code)

#Import Modules
import tkinter
import TkTreectrl
import sqlite3
#Database
db = sqlite3.connect("marks.db")
cursor=db.cursor()
print("Opened Database")
db.execute("CREATE TABLE IF NOT EXISTS marks (Firstname, Surname, Mark)")
print("Table created")
#Variables
Surname = str()
Firstname = str()
Mark = int()
#Define Buttons
def addentry() :
   db.execute('INSERT INTO marks VALUES (?,?,?);', (Firstname,Surname,Mark))
   db.commit()
   print ("Entry Added To Database")

def deleteentry() :
   selected=listbox.get(ACTIVE)
   name=selected[0]
   db.execute('DELETE FROM marks where Firstname =?', (name))
   print ("Entry Deleted")
#Window
window = tkinter.Tk()
window.title("marks")
window.geometry("875x500")
#Labels
lbl_surname = tkinter.Label(window, text="Surname:", font="Arial 12 bold")
lbl_first = tkinter.Label(window, text="First-Name:", font="Arial 12 bold")
lbl_mark = tkinter.Label(window, text="Mark (Percentage):", font="Arial 12 bold")
#Entry
txt_surname = tkinter.Entry(window, textvariable=Surname)
txt_first = tkinter.Entry(window, textvariable=Firstname)
txt_mark = tkinter.Entry(window, textvariable=Mark)
#Button
but_save = tkinter.Button(window, text="Save", command= addentry, width=10)
but_delete = tkinter.Button(window, text="Delete", command=deleteentry, width=10)
#List
listbox = TkTreectrl.MultiListbox(window)
listbox.config(columns=('Firstname', 'Surname', ' Mark'))
cursor.execute('SELECT * from marks')
for row in cursor.fetchall():
    listbox.insert('end',*map(unicode,row))
#Grid
lbl_surname.grid(row=0, column=1, padx=100)
lbl_first.grid(row=0, column=0)
lbl_mark.grid(row=0, column=2)
txt_surname.grid(row=1, column=1, padx=110)
txt_first.grid(row=1, column=0, padx=10)
txt_mark.grid(row=1, column=2)
but_save.grid(row=0, column=3)
but_delete.grid(row=1, column=3)
listbox.grid(row=2)
#Render
window.mainloop()

Any Ideas on what I've done wrong

Thanks Chris

6
  • Have you verified that Firstname, Surname, and Mark actually have values at the time you're doing the commit? Have you verified that fetchall() is actually returning a non-empty result? Commented May 28, 2015 at 20:48
  • It would appear that Firstname etc.. don't have values. So how would i go about making it so they day i assumed you could use the textvariable of an entry widget, but is there something else I have to do in order to retrieve the contents of the entry box? Commented May 28, 2015 at 22:18
  • Yes, you can use the textvariable. Without seeing your code it's impossible for me to guess what you're doing wrong. Commented May 28, 2015 at 23:05
  • The code for the entry widgets is: txt_surname = tkinter.Entry(window, textvariable=Surname) this the same for the other entries obviously just with different text variables. Commented May 28, 2015 at 23:57
  • Waffle, that's one extra line of code isn't helpful at all. If you want to add more code, edit the question and add a complete working program (also known as an MCVE: stackoverflow.com/help/mcve Commented May 29, 2015 at 4:20

1 Answer 1

0

You're using textvariable wrong. The value needs to be a special Tkinter variable such as StringVar. The second is that you have to use the get method on these variables to get the value. For example:

firstNameVar = tkinter.StringVar()
...
txt_first = tkinter.Entry(..., textvariable=firstNameVar)
...
Firstname = firstNameVar.get()
...
Sign up to request clarification or add additional context in comments.

2 Comments

I'm now getting the error: File "E:/Python/Competition/Competition.py", line 16, in <module> FirstnameVar = tkinter.StringVar() File "E:\Programs\Miniconda3\lib\tkinter_init_.py", line 287, in init Variable.__init__(self, master, value, name) File "E:\Programs\Miniconda3\lib\tkinter_init_.py", line 215, in init self._tk = master.tk AttributeError: 'NoneType' object has no attribute 'tk'
@TheEpicWaffle: you have to create the root window before you create the variables. This has been asked and answered on this site. stackoverflow.com/q/8762747/7432

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.