Senin, 27 Oktober 2025

PYTHON & SQLITE3 & WXPYTHON GUI

 




import wx

import sqlite3


class MyFrame(wx.Frame):

    def __init__(self, parent, title):

        super(MyFrame, self).__init__(parent, title=title, size=(400, 300))


        self.panel = wx.Panel(self)

        self.name_label = wx.StaticText(self.panel, label="Name:")

        self.name_text = wx.TextCtrl(self.panel)

        self.email_label = wx.StaticText(self.panel, label="Email:")

        self.email_text = wx.TextCtrl(self.panel)

        self.add_button = wx.Button(self.panel, label="Add User")

        self.display_button = wx.Button(self.panel, label="Display Users")

        self.output_text = wx.TextCtrl(self.panel, style=wx.TE_MULTILINE | wx.TE_READONLY)


        self.add_button.Bind(wx.EVT_BUTTON, self.on_add_user)

        self.display_button.Bind(wx.EVT_BUTTON, self.on_display_users)


        self.init_db()

        self.setup_layout()


    def init_db(self):

        self.conn = sqlite3.connect('my_wx_app_db.db')

        self.cursor = self.conn.cursor()

        self.cursor.execute('''

            CREATE TABLE IF NOT EXISTS users (

                id INTEGER PRIMARY KEY,

                name TEXT,

                email TEXT

            )

        ''')

        self.conn.commit()


    def setup_layout(self):

        sizer = wx.BoxSizer(wx.VERTICAL)

        input_sizer = wx.GridSizer(2, 2, 5, 5) # Rows, Cols, HGap, VGap


        input_sizer.Add(self.name_label, 0, wx.ALIGN_RIGHT)

        input_sizer.Add(self.name_text, 1, wx.EXPAND)

        input_sizer.Add(self.email_label, 0, wx.ALIGN_RIGHT)

        input_sizer.Add(self.email_text, 1, wx.EXPAND)


        button_sizer = wx.BoxSizer(wx.HORIZONTAL)

        button_sizer.Add(self.add_button, 0, wx.ALL, 5)

        button_sizer.Add(self.display_button, 0, wx.ALL, 5)


        sizer.Add(input_sizer, 0, wx.EXPAND | wx.ALL, 10)

        sizer.Add(button_sizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)

        sizer.Add(self.output_text, 1, wx.EXPAND | wx.ALL, 10)


        self.panel.SetSizer(sizer)

        self.Centre()

        self.Show()


    def on_add_user(self, event):

        name = self.name_text.GetValue()

        email = self.email_text.GetValue()

        if name and email:

            self.cursor.execute("INSERT INTO users (name, email) VALUES (?, ?)", (name, email))

            self.conn.commit()

            self.output_text.AppendText(f"User '{name}' added.\n")

            self.name_text.Clear()

            self.email_text.Clear()

        else:

            wx.MessageBox("Please enter both name and email.", "Input Error", wx.OK | wx.ICON_ERROR)


    def on_display_users(self, event):

        self.output_text.Clear()

        self.cursor.execute("SELECT * FROM users")

        rows = self.cursor.fetchall()

        if rows:

            self.output_text.AppendText("Current Users:\n")

            for row in rows:

                self.output_text.AppendText(f"ID: {row[0]}, Name: {row[1]}, Email: {row[2]}\n")

        else:

            self.output_text.AppendText("No users in the database.\n")


    def OnClose(self, event):

        self.conn.close()

        self.Destroy()


if __name__ == '__main__':

    app = wx.App(False)

    frame = MyFrame(None, "Simple SQLite3 wxPython App")

    app.MainLoop()


Tidak ada komentar: