1

I'm trying to work on a Tkinter GUI with a backed sqlite3 database. I want to control the database with Buttons on my Tkinter window. Also I'm figuring out, how to improve my code while using classes. However, if I'm pressing the button "create database" no table is inserted into the database.

Following my code:

import sqlite3
import datetime

import os.path

import tkinter as tk
from tkinter import *
from tkinter import messagebox
from tkinter.ttk import Treeview, Separator


class MainApplication():
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.master.title('Titanic DB')
        self.master.geometry('850x600')
        self.master.resizable(False, False)


        self.search_frame = tk.Frame(self.master).grid(row=0, column=0)
        self.label_search_heading = tk.Label(self.search_frame, text='DATABASE |', font=('bold', 14), pady=10).grid(row=0, column=0, sticky=W)

        self.button_create_database = tk.Button(self.search_frame, text='create database', command=self.ActionButtons, font=('bold', 14), pady=5, padx=50).grid(row=20, column=0, sticky=W)


    def ActionButtons(self):
        self.db = Database()
        self.createDB = self.db.CreateDatabase

class Database():

    DATABASE = "TitanicPassengers.db"

    def __init__(self):
        self.connection = sqlite3.connect(Database.DATABASE)
        self.cursor = self.connection.cursor()

    def CreateDatabase(self):
        self.connection = sqlite3.connect(Database.DATABASE)
        self.cursor = self.connection.cursor()
        self.execute("CREATE TABLE IF NOT EXISTS Passengers(Survived INTEGER, " \
                                                    "PClass INTEGER, " \
                                                    "Name TEXT, " \
                                                    "Sex TEXT, " \
                                                    "Age INTEGER, " \
                                                    "SiblingsSpousesAbord INTEGER, " \
                                                    "ParentsChildrenAbord INTEGER, "  \
                                                    "Fare REAL ) " 
                    )

        self.connection.commit()
        self.connection.close()

def main():

    root = tk.Tk()
    app = MainApplication(root)
    root.mainloop()


if __name__ == '__main__':
    main()

I would be glad, if someone could give me a hint, what I'm doing wrong. All additional tips how to organize my code better, are highly appreciated. Thanks!

2
  • By the way you aren't creating the <tk.Label> and <tk.Button> inside the frame. Split this line: self.search_frame = tk.Frame(self.master).grid(row=0, column=0) in 2. The .grid method always returns None. Commented Feb 21, 2021 at 12:20
  • 1
    You did not actually execute CreateDatabase() function in the line self.createDB = self.db.CreateDatabase because of missing (). It should be self.createDB = self.db.CreateDatabase(). Commented Feb 21, 2021 at 16:00

2 Answers 2

1

Well, I figured it out by myself.

I changed

self.createDB = self.db.CreateDatabase

to

self.db.CreateDatabase()

Nevermind, all tips for a better code structure are helpful.

Thanks!

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

Comments

0

I believe that

self.execute("CREATE TABLE IF NOT EXISTS...etc

should be

self.cursor.execute..

is not it?

Comments

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.