Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not Working
#1
When I run the following code it issues an error message. Can't figure it out.

from tkinter import *

class Application(Frame):
  def __init__(self, master=None):
    Frame.__init__(self, master)
    conn = self.create_connection()

  def create_connection():
    """ create a database connection to the SQLite database
        specified by the db_file
    :param db_file: database file
    :return: Connection object or None
    """
    try:
      filename = "\\\\MYBOOKLIVE\\Public\\Databases\\DM_Earnings_2019.db"
      conn = sqlite3.connect(filename)      
    except Error as e:
       print(e)
    finally:
      return conn

root  = Tk()
app = Application(master=root)
app.pack(side=TOP, fill=BOTH, expand=True)
root.mainloop()
I get the following message.......

Traceback (most recent call last):
File "//MYBOOKLIVE/Public/Code/New/a_test.py", line 23, in <module>
app = Application(master=root)
File "//MYBOOKLIVE/Public/Code/New/a_test.py", line 6, in __init__
conn = self.create_connection()
TypeError: create_connection() takes 0 positional arguments but 1 was given

I have specified zero arguments yet it still gets the error message. Please help.
Reply
#2
Because it's part of a class, there's an implicit self parameter that's passed. If you define the function as def create_connection(self):, your error will go away :)
Reply
#3
in addition to self being missing, you didn't import sqlite3
I also added a geometry statement for minimal size, adjust to what you like
from tkinter import *
import sqlite3

 
class Application(Frame):
  def __init__(self, master=None):
    Frame.__init__(self, master)
    master.geometry("400x200+30+30")
    conn = self.create_connection()
 
  def create_connection(self):
    """ create a database connection to the SQLite database
        specified by the db_file
    :param db_file: database file
    :return: Connection object or None
    """
    try:
      filename = "\\\\MYBOOKLIVE\\Public\\Databases\\DM_Earnings_2019.db"
      conn = sqlite3.connect(filename)      
    except UnboundLocalError:
       print('Unbound Loca error')
    finally:
      return conn
 
root  = Tk()
app = Application(master=root)
root.mainloop()
Reply
#4
thanks to both nilamo and Larx60+
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020