Python Forum
Why isn't this working. I've made it as simple as posible
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why isn't this working. I've made it as simple as posible
#1
from tkinter import *
from sqlite3 import *

def center(toplevel):     #Run outside the Class definition
  toplevel.update_idletasks()
  w = toplevel.winfo_screenwidth()
  h = toplevel.winfo_screenheight()
  size = tuple(int(_) for _ in toplevel.geometry().split('+')[0].split('x'))
  x = w/2 - size[0]/2
  y = h/2 - size[1]/2
  toplevel.geometry("%dx%d+%d+%d" % (size + (x, y)))

class Earnings():

  def __init__(self):
    print("About to go to self.connect_database")
    self.conn = self.connect_database(self)    

  def connect_database(self):
    #create a database connection
    print("About to go to self.create_connection")
 
def main():
  root = Tk()
  root.geometry("1250x650")
  root.title("The Ryall Family Earnings")
  center(root)
  App = Earnings()
  root.mainloop()
 
if __name__ == '__main__':
  main()
Why do I keep getting the following message when I only enter the "self" parm when I go to self.connect_database(self)

Error:
Traceback (most recent call last): File "D:/Code/New/test_class.py", line 32, in <module> main() File "D:/Code/New/test_class.py", line 28, in main App = Earnings() File "D:/Code/New/test_class.py", line 17, in __init__ self.conn = self.connect_database(self) TypeError: connect_database() takes 1 positional argument but 2 were given
Reply
#2
There's absolutely no need for a class here, but if you insist:
class Earnings:
  def __init__(self, dbname):
    print("Connecting to database")
    self.conn = sqlite3.connect(dbname)

#replace line 28 with:
App = Earnings('MyDatabaseName')
and has to be called passing dbname as an argument

this can be replaced with (placed on line 28) and eliminating the class:
conn = sqlite3.connect('MyDatabaseName')
Reply
#3
The error is because the line
self.conn = self.connect_database(self)
you don't pass self to self
change it to
self.conn = self.connect_database()
Reply
#4
Thank you Lar60+ and Yoriz
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple conditional not working as expected return2sender 8 934 Aug-27-2023, 10:39 PM
Last Post: return2sender
  So simple yet not working.... giladal 13 4,082 Oct-27-2020, 09:31 PM
Last Post: GOTO10
  Made a simple script for android (really simple) but it is not running anddontyoucomebacknomore 2 2,322 Mar-06-2019, 12:19 AM
Last Post: anddontyoucomebacknomore

Forum Jump:

User Panel Messages

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