1. Establishes database connectivity with SQLite.
Creates a
table, inserts records, and retrieves data from a database.
Displays the data in a GUI window using Tkinter. Threads the
database operation to keep the GUI responsive.
import sqlite3
import tkinter as tk
from tkinter import ttk
import threading
# Database setup
def setup_database():
conn = [Link]('[Link]')
c = [Link]()
[Link]('''CREATE TABLE IF NOT EXISTS users
(id INTEGER PRIMARY KEY, name TEXT, age INTEGER)''')
[Link]('''INSERT INTO users (name, age) VALUES
('Alice', 30),
('Bob', 25),
('Charlie', 35)''')
[Link]()
[Link]()
# Fetch data from the database
def fetch_data():
conn = [Link]('[Link]')
c = [Link]()
[Link]('SELECT * FROM users')
rows = [Link]()
[Link]()
return rows
# Update the GUI with the fetched data
def update_gui(tree):
for i in tree.get_children():
[Link](i)
for row in fetch_data():
[Link]('', [Link], values=row)
# Threaded database fetch
def threaded_fetch(tree):
[Link](target=lambda: update_gui(tree)).start()
# Main application window
class App([Link]):
def __init__(self):
super().__init__()
[Link]("SQLite and Tkinter Example")
[Link]("400x300")
[Link] = [Link](self, columns=('ID', 'Name', 'Age'), show='headings')
[Link]('ID', text='ID')
[Link]('Name', text='Name')
[Link]('Age', text='Age')
[Link](expand=True, fill=[Link])
self.refresh_button = [Link](self, text="Refresh", command=lambda:
threaded_fetch([Link]))
self.refresh_button.pack()
# Setup and run the application
if __name__ == "__main__":
setup_database()
app = App()
threaded_fetch([Link])
[Link]()