0

I want Python to display data from Access on Label. I tried to do smth like this but it doesn't work. There is no error but Label displays pypyodbc.Cursor object at 0x05E02990. What should I do? Thank you

from tkinter import *
import pypyodbc
import ctypes

#Create connection
con = pypyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)};UID=admin;UserCommitSync=Yes;Threads=3;SafeTransactions=0;PageTimeout=5;MaxScanRows=8;MaxBufferSize=2048;FIL={MS Access};DriverId=25;DefaultDir=C:/Users/HP/Desktop/PITL;DBQ=C:/Users/HP/Desktop/PITL/PITL.mdb;')
cursor = con.cursor ()

form = Tk ()
form.title ("Main")
form.geometry ('400x400')

form2 = Tk ()
form2.title ("Main")
form2.geometry ('400x400')
form2.withdraw()

def Show():
    cursor.execute ("SELECT Law_ID FROM Laws WHERE Fine=1")
    a=cursor.execute
    for a in cursor:
        print ("Law ID where Fine is 1 is", a)
        Label(form2, text=cursor).pack() 
    form.withdraw()
    form2.deiconify()

Button(form, text = 'PUSH ME', command = Show).pack()

form.mainloop ()

con.commit ()
cursor.close ()
con.close ()
3
  • What happens when you replace print ("Law ID where Fine is 1 is", a) with print ("Law ID where Fine is 1 is", list(a))? Commented Feb 18, 2018 at 8:21
  • @Deadlock It doesn't change text in Label Commented Feb 18, 2018 at 9:06
  • Possible duplicate of Python cannot display data on Label twice Commented Feb 18, 2018 at 13:11

2 Answers 2

2

The error is because you are printing the value of cursor object which always be a location, in your case "0x05E02990". To get the value retrieved by the cursor you have to write:

variable_name = cursor.fetchall()

In your code:

def Show():
    cursor.execute ("SELECT Law_ID FROM Laws WHERE Fine=1")
    a=cursor.fetchall()
    ##print as you like    

It will fetch all the data retrieved by cursor

Sign up to request clarification or add additional context in comments.

Comments

0

Try this one on your function

def Show():

    cursor.execute ("SELECT Law_ID FROM Laws WHERE Fine=1")
    a=cursor.fetchall()

    for i in range(len(a)):
        print ("Law ID where Fine is 1 is", i)
        Label(form2, text=cursor).grid(row = i, column = 0)

1 Comment

Your solution still passes cursor object in Label().

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.