Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sqlite not updating
#11
(May-25-2023, 08:47 PM)angus1964 Wrote:
(May-24-2023, 07:02 PM)menator01 Wrote: I tried running your code, the database was created but not the tables.

Hi Menator
I moved the call to create the db to just below menu creation, and it works fine now, not a great way to do it, will investigates your class method.
Still have the original issue, did wonder if it was due to files being on cloud but when run locally same issue. Hoping once class built it will solve it.
Have managed to work out when you click the save button it is added to the database, I checked from the terminal. So it would appear the issue is getting the full data to populate the table.

Confused

Think I have figured it out, all the code is in the init section so will be created at startup. Going to create a class which is called from a button on the page.
Reply
#12
I've not ever really messed with tkinters menu or tkraise before so, I gave it a try with some of your code.

import tkinter as tk
import sqlite3 as sq

class Database:
    '''
        Database class handles all the database functions.
        Create, insert, and retrieve
    '''
    def __init__(self):
        self.tables = ('rtbhigh', 'finisheshigh')
        self.connection = sq.connect('highscores.db')
        self.cursor = self.connection.cursor()

    def create_tables(self):
        self.cursor.execute('''
            create table if not exists rtbhigh (
            playername text, score integer
            )
        ''')

        self.cursor.execute('''
            create table if not exists finisheshigh (
            playername text, score integer
            )
        ''')
        self.connection.commit()


    def insert(self, playername, score, table):
        self.cursor.execute(f'insert into {table} (playername, score) values(?,?)', (playername, score))
        self.connection.commit()
            
    def getall(self, table):
        query = self.cursor.execute(f'select * from {table} order by score desc').fetchall()
        print(query)
        return query


class Rtb(tk.Frame):
    def __init__(self):
        tk.Frame.__init__(self)
        label = tk.Label(self, text='Rtb Page', bg='pink')
        label.grid(column=0, row=0, sticky='news')


class Main(tk.Frame):
    ''' Main class is the start/landing page '''
    def __init__(self):
        tk.Frame.__init__(self)
        label = tk.Label(self, text='Main Page', bg='gold')
        label.grid(column=0, row=0, sticky='news')


class Finish(tk.Frame):
    def __init__(self):
        tk.Frame.__init__(self)
        label = tk.Label(self, text='Finish Page', bg='tomato')
        label.grid(column=0, row=0, sticky='news')


class HighScore(tk.Frame):
    '''
        HighScores class displays all scores
    '''
    def __init__(self, data):
        tk.Frame.__init__(self)
        container = tk.Frame(self)
        container.grid(column=0, row=0, sticky='new')
        container.grid_columnconfigure(0, weight=3)

        header = tk.Label(container, text='High Scores', bg='gray')
        header['font'] = (None, 18, 'bold')
        header['highlightbackground'] = 'black'
        header['highlightcolor'] = 'black'
        header['highlightthickness'] = 1
        header.grid(column=0, row=0, sticky='new', padx=3, pady=(0, 8))

        row = 1
        for name, score in data:
            label = tk.Label(container, text=f'{name.title()}: {score}', bg='lightgray', anchor='w', padx=8)
            label['font'] = (None, 14, 'normal')
            label['highlightbackground'] = 'black'
            label['highlightcolor'] = 'black'
            label['highlightthickness'] = 1
            label.grid(column=0, row=row, sticky='new', ipadx=5, padx=3)
            row += 1


class NewGame(tk.Frame):
    ''' NewGame class page for starting new games '''
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        label = tk.Label(self, text='Start a new game', bg='burlywood')
        label['font'] = (None, 16, 'normal')
        label.grid(column=0, row=0, sticky='new')


class Window(tk.Frame):
    ''' Window class is for the menus and container'''
    def __init__(self, parent):
        self.parent = parent
        tk.Frame.__init__(self, parent)
        parent.title('Test Application')
        parent.columnconfigure(0, weight=1)
        parent.rowconfigure(0, weight=1)

        self.menubar = tk.Menu(parent)
        parent.configure(menu=self.menubar)



class Controller:
    ''' Controller class ties all the classes together '''
    def __init__(self, database, window):
        self.db = database
        self.window = window
        self.db.create_tables()
        # self.db.insert('john doe', 15, self.db.tables[1])
        # self.db.insert('jane doe', 25, self.db.tables[1])
        # self.db.insert('child doe', 5, self.db.tables[1])
        

        # Setup the pages by calling the appropiate class
        page2 = Rtb()
        page2.grid(column=0, row=0, sticky='news')
        page2.grid_columnconfigure(0, weight=3)
        page2.grid_rowconfigure(0, weight=3)

        page3 = Finish()
        page3.grid(column=0, row=0, sticky='news')
        page3.grid_columnconfigure(0, weight=3)
        page3.grid_rowconfigure(0, weight=3)

        page4 = HighScore(self.get_scores())
        page4.grid(column=0, row=0, sticky='news')
        page4.grid_columnconfigure(0, weight=3)
        page4.grid_rowconfigure(0, weight=3)

        page5 = NewGame()
        page5.grid(column=0, row=0, sticky='news')
        page5.grid_columnconfigure(0, weight=3)
        page5.grid_rowconfigure(0, weight=3)

        page1 = Main()
        page1.grid(column=0, row=0, sticky='news')
        page1.grid_columnconfigure(0, weight=3)
        page1.grid_rowconfigure(0, weight=3)

        # Setup the menus and commands
        game_menu = tk.Menu(self.window.menubar, tearoff=0)
        game_menu.add_command(label='New Game', command=page5.tkraise)
        game_menu.add_separator()
        game_menu.add_command(label='Exit', command=self.window.parent.destroy)

        page_menu = tk.Menu(self.window.menubar, tearoff=0)
        page_menu.add_command(label='Main Page', command=page1.tkraise)
        page_menu.add_command(label='RTB Page', command=page2.tkraise)
        page_menu.add_command(label='Finish Page', command=page3.tkraise)
        page_menu.add_command(label='High Scores', command=page4.tkraise)

        page_menu.add_separator()
        page_menu.add_command(label='Exit', command=self.window.parent.destroy)

        self.window.menubar.add_cascade(label='Game Options', menu=game_menu)
        self.window.menubar.add_cascade(label='Pages', menu=page_menu)

    def get_scores(self):
        ''' Method for getting scores '''
        data = self.db.getall(self.db.tables[1])
        print(data)
        return data


if __name__ == '__main__':
    root = tk.Tk()
    root.geometry('600x400+300+300')
    controller = Controller(Database(), Window(root))
    root.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#13
(May-27-2023, 09:05 PM)menator01 Wrote: I've not ever really messed with tkinters menu or tkraise before so, I gave it a try with some of your code.

import tkinter as tk
import sqlite3 as sq

class Database:
    '''
        Database class handles all the database functions.
        Create, insert, and retrieve
    '''
    def __init__(self):
        self.tables = ('rtbhigh', 'finisheshigh')
        self.connection = sq.connect('highscores.db')
        self.cursor = self.connection.cursor()

    def create_tables(self):
        self.cursor.execute('''
            create table if not exists rtbhigh (
            playername text, score integer
            )
        ''')

        self.cursor.execute('''
            create table if not exists finisheshigh (
            playername text, score integer
            )
        ''')
        self.connection.commit()


    def insert(self, playername, score, table):
        self.cursor.execute(f'insert into {table} (playername, score) values(?,?)', (playername, score))
        self.connection.commit()
            
    def getall(self, table):
        query = self.cursor.execute(f'select * from {table} order by score desc').fetchall()
        print(query)
        return query


class Rtb(tk.Frame):
    def __init__(self):
        tk.Frame.__init__(self)
        label = tk.Label(self, text='Rtb Page', bg='pink')
        label.grid(column=0, row=0, sticky='news')


class Main(tk.Frame):
    ''' Main class is the start/landing page '''
    def __init__(self):
        tk.Frame.__init__(self)
        label = tk.Label(self, text='Main Page', bg='gold')
        label.grid(column=0, row=0, sticky='news')


class Finish(tk.Frame):
    def __init__(self):
        tk.Frame.__init__(self)
        label = tk.Label(self, text='Finish Page', bg='tomato')
        label.grid(column=0, row=0, sticky='news')


class HighScore(tk.Frame):
    '''
        HighScores class displays all scores
    '''
    def __init__(self, data):
        tk.Frame.__init__(self)
        container = tk.Frame(self)
        container.grid(column=0, row=0, sticky='new')
        container.grid_columnconfigure(0, weight=3)

        header = tk.Label(container, text='High Scores', bg='gray')
        header['font'] = (None, 18, 'bold')
        header['highlightbackground'] = 'black'
        header['highlightcolor'] = 'black'
        header['highlightthickness'] = 1
        header.grid(column=0, row=0, sticky='new', padx=3, pady=(0, 8))

        row = 1
        for name, score in data:
            label = tk.Label(container, text=f'{name.title()}: {score}', bg='lightgray', anchor='w', padx=8)
            label['font'] = (None, 14, 'normal')
            label['highlightbackground'] = 'black'
            label['highlightcolor'] = 'black'
            label['highlightthickness'] = 1
            label.grid(column=0, row=row, sticky='new', ipadx=5, padx=3)
            row += 1


class NewGame(tk.Frame):
    ''' NewGame class page for starting new games '''
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        label = tk.Label(self, text='Start a new game', bg='burlywood')
        label['font'] = (None, 16, 'normal')
        label.grid(column=0, row=0, sticky='new')


class Window(tk.Frame):
    ''' Window class is for the menus and container'''
    def __init__(self, parent):
        self.parent = parent
        tk.Frame.__init__(self, parent)
        parent.title('Test Application')
        parent.columnconfigure(0, weight=1)
        parent.rowconfigure(0, weight=1)

        self.menubar = tk.Menu(parent)
        parent.configure(menu=self.menubar)



class Controller:
    ''' Controller class ties all the classes together '''
    def __init__(self, database, window):
        self.db = database
        self.window = window
        self.db.create_tables()
        # self.db.insert('john doe', 15, self.db.tables[1])
        # self.db.insert('jane doe', 25, self.db.tables[1])
        # self.db.insert('child doe', 5, self.db.tables[1])
        

        # Setup the pages by calling the appropiate class
        page2 = Rtb()
        page2.grid(column=0, row=0, sticky='news')
        page2.grid_columnconfigure(0, weight=3)
        page2.grid_rowconfigure(0, weight=3)

        page3 = Finish()
        page3.grid(column=0, row=0, sticky='news')
        page3.grid_columnconfigure(0, weight=3)
        page3.grid_rowconfigure(0, weight=3)

        page4 = HighScore(self.get_scores())
        page4.grid(column=0, row=0, sticky='news')
        page4.grid_columnconfigure(0, weight=3)
        page4.grid_rowconfigure(0, weight=3)

        page5 = NewGame()
        page5.grid(column=0, row=0, sticky='news')
        page5.grid_columnconfigure(0, weight=3)
        page5.grid_rowconfigure(0, weight=3)

        page1 = Main()
        page1.grid(column=0, row=0, sticky='news')
        page1.grid_columnconfigure(0, weight=3)
        page1.grid_rowconfigure(0, weight=3)

        # Setup the menus and commands
        game_menu = tk.Menu(self.window.menubar, tearoff=0)
        game_menu.add_command(label='New Game', command=page5.tkraise)
        game_menu.add_separator()
        game_menu.add_command(label='Exit', command=self.window.parent.destroy)

        page_menu = tk.Menu(self.window.menubar, tearoff=0)
        page_menu.add_command(label='Main Page', command=page1.tkraise)
        page_menu.add_command(label='RTB Page', command=page2.tkraise)
        page_menu.add_command(label='Finish Page', command=page3.tkraise)
        page_menu.add_command(label='High Scores', command=page4.tkraise)

        page_menu.add_separator()
        page_menu.add_command(label='Exit', command=self.window.parent.destroy)

        self.window.menubar.add_cascade(label='Game Options', menu=game_menu)
        self.window.menubar.add_cascade(label='Pages', menu=page_menu)

    def get_scores(self):
        ''' Method for getting scores '''
        data = self.db.getall(self.db.tables[1])
        print(data)
        return data


if __name__ == '__main__':
    root = tk.Tk()
    root.geometry('600x400+300+300')
    controller = Controller(Database(), Window(root))
    root.mainloop()
I did move the database into a class and that solved the problem.
In your code above I can see a structure style and elegance that does not exist in my code. Planning to use the above as a starting block and build around it while trying to mirror the style and structure.

Many thanks.

Angus
menator01 likes this post
Reply
#14
(May-28-2023, 10:04 AM)angus1964 Wrote:
(May-27-2023, 09:05 PM)menator01 Wrote: I've not ever really messed with tkinters menu or tkraise before so, I gave it a try with some of your code.

import tkinter as tk
import sqlite3 as sq

class Database:
    '''
        Database class handles all the database functions.
        Create, insert, and retrieve
    '''
    def __init__(self):
        self.tables = ('rtbhigh', 'finisheshigh')
        self.connection = sq.connect('highscores.db')
        self.cursor = self.connection.cursor()

    def create_tables(self):
        self.cursor.execute('''
            create table if not exists rtbhigh (
            playername text, score integer
            )
        ''')

        self.cursor.execute('''
            create table if not exists finisheshigh (
            playername text, score integer
            )
        ''')
        self.connection.commit()


    def insert(self, playername, score, table):
        self.cursor.execute(f'insert into {table} (playername, score) values(?,?)', (playername, score))
        self.connection.commit()
            
    def getall(self, table):
        query = self.cursor.execute(f'select * from {table} order by score desc').fetchall()
        print(query)
        return query


class Rtb(tk.Frame):
    def __init__(self):
        tk.Frame.__init__(self)
        label = tk.Label(self, text='Rtb Page', bg='pink')
        label.grid(column=0, row=0, sticky='news')


class Main(tk.Frame):
    ''' Main class is the start/landing page '''
    def __init__(self):
        tk.Frame.__init__(self)
        label = tk.Label(self, text='Main Page', bg='gold')
        label.grid(column=0, row=0, sticky='news')


class Finish(tk.Frame):
    def __init__(self):
        tk.Frame.__init__(self)
        label = tk.Label(self, text='Finish Page', bg='tomato')
        label.grid(column=0, row=0, sticky='news')


class HighScore(tk.Frame):
    '''
        HighScores class displays all scores
    '''
    def __init__(self, data):
        tk.Frame.__init__(self)
        container = tk.Frame(self)
        container.grid(column=0, row=0, sticky='new')
        container.grid_columnconfigure(0, weight=3)

        header = tk.Label(container, text='High Scores', bg='gray')
        header['font'] = (None, 18, 'bold')
        header['highlightbackground'] = 'black'
        header['highlightcolor'] = 'black'
        header['highlightthickness'] = 1
        header.grid(column=0, row=0, sticky='new', padx=3, pady=(0, 8))

        row = 1
        for name, score in data:
            label = tk.Label(container, text=f'{name.title()}: {score}', bg='lightgray', anchor='w', padx=8)
            label['font'] = (None, 14, 'normal')
            label['highlightbackground'] = 'black'
            label['highlightcolor'] = 'black'
            label['highlightthickness'] = 1
            label.grid(column=0, row=row, sticky='new', ipadx=5, padx=3)
            row += 1


class NewGame(tk.Frame):
    ''' NewGame class page for starting new games '''
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        label = tk.Label(self, text='Start a new game', bg='burlywood')
        label['font'] = (None, 16, 'normal')
        label.grid(column=0, row=0, sticky='new')


class Window(tk.Frame):
    ''' Window class is for the menus and container'''
    def __init__(self, parent):
        self.parent = parent
        tk.Frame.__init__(self, parent)
        parent.title('Test Application')
        parent.columnconfigure(0, weight=1)
        parent.rowconfigure(0, weight=1)

        self.menubar = tk.Menu(parent)
        parent.configure(menu=self.menubar)



class Controller:
    ''' Controller class ties all the classes together '''
    def __init__(self, database, window):
        self.db = database
        self.window = window
        self.db.create_tables()
        # self.db.insert('john doe', 15, self.db.tables[1])
        # self.db.insert('jane doe', 25, self.db.tables[1])
        # self.db.insert('child doe', 5, self.db.tables[1])
        

        # Setup the pages by calling the appropiate class
        page2 = Rtb()
        page2.grid(column=0, row=0, sticky='news')
        page2.grid_columnconfigure(0, weight=3)
        page2.grid_rowconfigure(0, weight=3)

        page3 = Finish()
        page3.grid(column=0, row=0, sticky='news')
        page3.grid_columnconfigure(0, weight=3)
        page3.grid_rowconfigure(0, weight=3)

        page4 = HighScore(self.get_scores())
        page4.grid(column=0, row=0, sticky='news')
        page4.grid_columnconfigure(0, weight=3)
        page4.grid_rowconfigure(0, weight=3)

        page5 = NewGame()
        page5.grid(column=0, row=0, sticky='news')
        page5.grid_columnconfigure(0, weight=3)
        page5.grid_rowconfigure(0, weight=3)

        page1 = Main()
        page1.grid(column=0, row=0, sticky='news')
        page1.grid_columnconfigure(0, weight=3)
        page1.grid_rowconfigure(0, weight=3)

        # Setup the menus and commands
        game_menu = tk.Menu(self.window.menubar, tearoff=0)
        game_menu.add_command(label='New Game', command=page5.tkraise)
        game_menu.add_separator()
        game_menu.add_command(label='Exit', command=self.window.parent.destroy)

        page_menu = tk.Menu(self.window.menubar, tearoff=0)
        page_menu.add_command(label='Main Page', command=page1.tkraise)
        page_menu.add_command(label='RTB Page', command=page2.tkraise)
        page_menu.add_command(label='Finish Page', command=page3.tkraise)
        page_menu.add_command(label='High Scores', command=page4.tkraise)

        page_menu.add_separator()
        page_menu.add_command(label='Exit', command=self.window.parent.destroy)

        self.window.menubar.add_cascade(label='Game Options', menu=game_menu)
        self.window.menubar.add_cascade(label='Pages', menu=page_menu)

    def get_scores(self):
        ''' Method for getting scores '''
        data = self.db.getall(self.db.tables[1])
        print(data)
        return data


if __name__ == '__main__':
    root = tk.Tk()
    root.geometry('600x400+300+300')
    controller = Controller(Database(), Window(root))
    root.mainloop()
I did move the database into a class and that solved the problem.
In your code above I can see a structure style and elegance that does not exist in my code. Planning to use the above as a starting block and build around it while trying to mirror the style and structure.

Many thanks.

Angus


Hi Menator

I was wondering if there was an easy to raise a page from a button on an other page?
Also I don't see where "Main" page is shown as the first page, am I missing something obvious there?

Angus
Reply
#15
(May-30-2023, 07:36 PM)angus1964 Wrote:
(May-28-2023, 10:04 AM)angus1964 Wrote: I did move the database into a class and that solved the problem.
In your code above I can see a structure style and elegance that does not exist in my code. Planning to use the above as a starting block and build around it while trying to mirror the style and structure.

Many thanks.

Angus


Hi Menator

I was wondering if there was an easy to raise a page from a button on an other page?
Also I don't see where "Main" page is shown as the first page, am I missing something obvious there?

Angus
Scratch the second part, I see it is from the order they are created in.
Reply
#16
Button command should be called the same as in the menu
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#17
I was also facing same issue, thanks for replies
Reply
#18
(May-30-2023, 08:23 PM)menator01 Wrote: Button command should be called the same as in the menu

If were calling it from a different class, how would you call it.
Have tried.
 controller.self.page1.tkraise()
But that did work.
Reply
#19
From my earlier example. On the rtb page I've placed two buttons. One will raise the main page and the other will raise the finish page.

import tkinter as tk
import sqlite3 as sq
 
class Database:
    '''
        Database class handles all the database functions.
        Create, insert, and retrieve
    '''
    def __init__(self):
        self.tables = ('rtbhigh', 'finisheshigh')
        self.connection = sq.connect('highscores.db')
        self.cursor = self.connection.cursor()
 
    def create_tables(self):
        self.cursor.execute('''
            create table if not exists rtbhigh (
            playername text, score integer
            )
        ''')
 
        self.cursor.execute('''
            create table if not exists finisheshigh (
            playername text, score integer
            )
        ''')
        self.connection.commit()
 
 
    def insert(self, playername, score, table):
        self.cursor.execute(f'insert into {table} (playername, score) values(?,?)', (playername, score))
        self.connection.commit()
             
    def getall(self, table):
        query = self.cursor.execute(f'select * from {table} order by score desc').fetchall()
        print(query)
        return query
 
 
class Rtb(tk.Frame):
    def __init__(self):
        tk.Frame.__init__(self)
        self.grid_columnconfigure(0, weight=3, uniform='btns')
        self.grid_columnconfigure(1, weight=3, uniform='btns')
        
        label = tk.Label(self, text='Rtb Page', bg='pink')
        label.grid(column=0, row=0, columnspan=2, sticky='news')

        self.button = tk.Button(self, text='Raise Main Page')
        self.button.grid(column=0, row=1, sticky='new', padx=2)

        self.button2 = tk.Button(self, text='Raise Finish Page')
        self.button2.grid(column=1, row=1, sticky='new', padx=2)
 
 
class Main(tk.Frame):
    ''' Main class is the start/landing page '''
    def __init__(self):
        tk.Frame.__init__(self)
        label = tk.Label(self, text='Main Page', bg='gold')
        label.grid(column=0, row=0, sticky='news')
 
 
class Finish(tk.Frame):
    def __init__(self):
        tk.Frame.__init__(self)
        label = tk.Label(self, text='Finish Page', bg='tomato')
        label.grid(column=0, row=0, sticky='news')
 
 
class HighScore(tk.Frame):
    '''
        HighScores class displays all scores
    '''
    def __init__(self, data):
        tk.Frame.__init__(self)
        container = tk.Frame(self)
        container.grid(column=0, row=0, sticky='new')
        container.grid_columnconfigure(0, weight=3)
 
        header = tk.Label(container, text='High Scores', bg='gray')
        header['font'] = (None, 18, 'bold')
        header['highlightbackground'] = 'black'
        header['highlightcolor'] = 'black'
        header['highlightthickness'] = 1
        header.grid(column=0, row=0, sticky='new', padx=3, pady=(0, 8))
 
        row = 1
        for name, score in data:
            label = tk.Label(container, text=f'{name.title()}: {score}', bg='lightgray', anchor='w', padx=8)
            label['font'] = (None, 14, 'normal')
            label['highlightbackground'] = 'black'
            label['highlightcolor'] = 'black'
            label['highlightthickness'] = 1
            label.grid(column=0, row=row, sticky='new', ipadx=5, padx=3)
            row += 1
 
 
class NewGame(tk.Frame):
    ''' NewGame class page for starting new games '''
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        label = tk.Label(self, text='Start a new game', bg='burlywood')
        label['font'] = (None, 16, 'normal')
        label.grid(column=0, row=0, sticky='new')
 
 
class Window(tk.Frame):
    ''' Window class is for the menus and container'''
    def __init__(self, parent):
        self.parent = parent
        tk.Frame.__init__(self, parent)
        parent.title('Test Application')
        parent.columnconfigure(0, weight=1)
        parent.rowconfigure(0, weight=1)
 
        self.menubar = tk.Menu(parent)
        parent.configure(menu=self.menubar)
 
 
 
class Controller:
    ''' Controller class ties all the classes together '''
    def __init__(self, database, window):
        self.db = database
        self.window = window
        self.db.create_tables()
        # self.db.insert('john doe', 15, self.db.tables[1])
        # self.db.insert('jane doe', 25, self.db.tables[1])
        # self.db.insert('child doe', 5, self.db.tables[1])
         
 
        # Setup the pages by calling the appropiate class
        page2 = Rtb()
        page2.grid(column=0, row=0, sticky='news')
        page2.grid_columnconfigure(0, weight=3)
        page2.grid_rowconfigure(0, weight=3)
 
        page3 = Finish()
        page3.grid(column=0, row=0, sticky='news')
        page3.grid_columnconfigure(0, weight=3)
        page3.grid_rowconfigure(0, weight=3)
 
        page4 = HighScore(self.get_scores())
        page4.grid(column=0, row=0, sticky='news')
        page4.grid_columnconfigure(0, weight=3)
        page4.grid_rowconfigure(0, weight=3)
 
        page5 = NewGame()
        page5.grid(column=0, row=0, sticky='news')
        page5.grid_columnconfigure(0, weight=3)
        page5.grid_rowconfigure(0, weight=3)
 
        page1 = Main()
        page1.grid(column=0, row=0, sticky='news')
        page1.grid_columnconfigure(0, weight=3)
        page1.grid_rowconfigure(0, weight=3)
 
        # Setup the menus and commands
        game_menu = tk.Menu(self.window.menubar, tearoff=0)
        game_menu.add_command(label='New Game', command=page5.tkraise)
        game_menu.add_separator()
        game_menu.add_command(label='Exit', command=self.window.parent.destroy)
 
        page_menu = tk.Menu(self.window.menubar, tearoff=0)
        page_menu.add_command(label='Main Page', command=page1.tkraise)
        page_menu.add_command(label='RTB Page', command=page2.tkraise)
        page_menu.add_command(label='Finish Page', command=page3.tkraise)
        page_menu.add_command(label='High Scores', command=page4.tkraise)
 
        page_menu.add_separator()
        page_menu.add_command(label='Exit', command=self.window.parent.destroy)
 
        self.window.menubar.add_cascade(label='Game Options', menu=game_menu)
        self.window.menubar.add_cascade(label='Pages', menu=page_menu)

        page2.button['command'] = page1.tkraise
        page2.button2['command'] = page3.tkraise
 
    def get_scores(self):
        ''' Method for getting scores '''
        data = self.db.getall(self.db.tables[1])
        print(data)
        return data
 
 
if __name__ == '__main__':
    root = tk.Tk()
    root.geometry('600x400+300+300')
    controller = Controller(Database(), Window(root))
    root.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#20
(May-31-2023, 06:11 AM)menator01 Wrote: From my earlier example. On the rtb page I've placed two buttons. One will raise the main page and the other will raise the finish page.

import tkinter as tk
import sqlite3 as sq
 
class Database:
    '''
        Database class handles all the database functions.
        Create, insert, and retrieve
    '''
    def __init__(self):
        self.tables = ('rtbhigh', 'finisheshigh')
        self.connection = sq.connect('highscores.db')
        self.cursor = self.connection.cursor()
 
    def create_tables(self):
        self.cursor.execute('''
            create table if not exists rtbhigh (
            playername text, score integer
            )
        ''')
 
        self.cursor.execute('''
            create table if not exists finisheshigh (
            playername text, score integer
            )
        ''')
        self.connection.commit()
 
 
    def insert(self, playername, score, table):
        self.cursor.execute(f'insert into {table} (playername, score) values(?,?)', (playername, score))
        self.connection.commit()
             
    def getall(self, table):
        query = self.cursor.execute(f'select * from {table} order by score desc').fetchall()
        print(query)
        return query
 
 
class Rtb(tk.Frame):
    def __init__(self):
        tk.Frame.__init__(self)
        self.grid_columnconfigure(0, weight=3, uniform='btns')
        self.grid_columnconfigure(1, weight=3, uniform='btns')
        
        label = tk.Label(self, text='Rtb Page', bg='pink')
        label.grid(column=0, row=0, columnspan=2, sticky='news')

        self.button = tk.Button(self, text='Raise Main Page')
        self.button.grid(column=0, row=1, sticky='new', padx=2)

        self.button2 = tk.Button(self, text='Raise Finish Page')
        self.button2.grid(column=1, row=1, sticky='new', padx=2)
 
 
class Main(tk.Frame):
    ''' Main class is the start/landing page '''
    def __init__(self):
        tk.Frame.__init__(self)
        label = tk.Label(self, text='Main Page', bg='gold')
        label.grid(column=0, row=0, sticky='news')
 
 
class Finish(tk.Frame):
    def __init__(self):
        tk.Frame.__init__(self)
        label = tk.Label(self, text='Finish Page', bg='tomato')
        label.grid(column=0, row=0, sticky='news')
 
 
class HighScore(tk.Frame):
    '''
        HighScores class displays all scores
    '''
    def __init__(self, data):
        tk.Frame.__init__(self)
        container = tk.Frame(self)
        container.grid(column=0, row=0, sticky='new')
        container.grid_columnconfigure(0, weight=3)
 
        header = tk.Label(container, text='High Scores', bg='gray')
        header['font'] = (None, 18, 'bold')
        header['highlightbackground'] = 'black'
        header['highlightcolor'] = 'black'
        header['highlightthickness'] = 1
        header.grid(column=0, row=0, sticky='new', padx=3, pady=(0, 8))
 
        row = 1
        for name, score in data:
            label = tk.Label(container, text=f'{name.title()}: {score}', bg='lightgray', anchor='w', padx=8)
            label['font'] = (None, 14, 'normal')
            label['highlightbackground'] = 'black'
            label['highlightcolor'] = 'black'
            label['highlightthickness'] = 1
            label.grid(column=0, row=row, sticky='new', ipadx=5, padx=3)
            row += 1
 
 
class NewGame(tk.Frame):
    ''' NewGame class page for starting new games '''
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        label = tk.Label(self, text='Start a new game', bg='burlywood')
        label['font'] = (None, 16, 'normal')
        label.grid(column=0, row=0, sticky='new')
 
 
class Window(tk.Frame):
    ''' Window class is for the menus and container'''
    def __init__(self, parent):
        self.parent = parent
        tk.Frame.__init__(self, parent)
        parent.title('Test Application')
        parent.columnconfigure(0, weight=1)
        parent.rowconfigure(0, weight=1)
 
        self.menubar = tk.Menu(parent)
        parent.configure(menu=self.menubar)
 
 
 
class Controller:
    ''' Controller class ties all the classes together '''
    def __init__(self, database, window):
        self.db = database
        self.window = window
        self.db.create_tables()
        # self.db.insert('john doe', 15, self.db.tables[1])
        # self.db.insert('jane doe', 25, self.db.tables[1])
        # self.db.insert('child doe', 5, self.db.tables[1])
         
 
        # Setup the pages by calling the appropiate class
        page2 = Rtb()
        page2.grid(column=0, row=0, sticky='news')
        page2.grid_columnconfigure(0, weight=3)
        page2.grid_rowconfigure(0, weight=3)
 
        page3 = Finish()
        page3.grid(column=0, row=0, sticky='news')
        page3.grid_columnconfigure(0, weight=3)
        page3.grid_rowconfigure(0, weight=3)
 
        page4 = HighScore(self.get_scores())
        page4.grid(column=0, row=0, sticky='news')
        page4.grid_columnconfigure(0, weight=3)
        page4.grid_rowconfigure(0, weight=3)
 
        page5 = NewGame()
        page5.grid(column=0, row=0, sticky='news')
        page5.grid_columnconfigure(0, weight=3)
        page5.grid_rowconfigure(0, weight=3)
 
        page1 = Main()
        page1.grid(column=0, row=0, sticky='news')
        page1.grid_columnconfigure(0, weight=3)
        page1.grid_rowconfigure(0, weight=3)
 
        # Setup the menus and commands
        game_menu = tk.Menu(self.window.menubar, tearoff=0)
        game_menu.add_command(label='New Game', command=page5.tkraise)
        game_menu.add_separator()
        game_menu.add_command(label='Exit', command=self.window.parent.destroy)
 
        page_menu = tk.Menu(self.window.menubar, tearoff=0)
        page_menu.add_command(label='Main Page', command=page1.tkraise)
        page_menu.add_command(label='RTB Page', command=page2.tkraise)
        page_menu.add_command(label='Finish Page', command=page3.tkraise)
        page_menu.add_command(label='High Scores', command=page4.tkraise)
 
        page_menu.add_separator()
        page_menu.add_command(label='Exit', command=self.window.parent.destroy)
 
        self.window.menubar.add_cascade(label='Game Options', menu=game_menu)
        self.window.menubar.add_cascade(label='Pages', menu=page_menu)

        page2.button['command'] = page1.tkraise
        page2.button2['command'] = page3.tkraise
 
    def get_scores(self):
        ''' Method for getting scores '''
        data = self.db.getall(self.db.tables[1])
        print(data)
        return data
 
 
if __name__ == '__main__':
    root = tk.Tk()
    root.geometry('600x400+300+300')
    controller = Controller(Database(), Window(root))
    root.mainloop()

Many thanks, so obvious now I see it.
Good example of why not to look at it at night, when your knackered. Cool
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Updating records 1 to n on an SQLite table KevinBrown 2 2,685 Mar-30-2019, 05:02 PM
Last Post: KevinBrown

Forum Jump:

User Panel Messages

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