Commit ff07bc2a authored by Chris Mayo's avatar Chris Mayo
Browse files

examples: Implement SimpleUIForm/example.c in Python

parent 5d569e4c
Pipeline #261940 passed with stages
in 18 minutes and 6 seconds
#!/usr/bin/python3
import gi
gi.require_version("Gda", "6.0")
gi.require_version("Gdaui", "6.0")
gi.require_version("Gtk", "3.0")
from gi.repository import Gda, Gdaui, Gtk
def critical_error(text, secondary_text=None):
dialog = Gtk.MessageDialog(
title="SimpleUIForm",
text=text,
secondary_text=secondary_text,
message_type=Gtk.MessageType.ERROR,
buttons=Gtk.ButtonsType.CLOSE,
)
dialog.run()
dialog.destroy()
raise SystemExit(1)
class SimpleUIForm(Gtk.Window):
def __init__(self):
super().__init__(title="example", border_width=10)
dsn = "SalesTest"
dsn_info = Gda.Config.get_dsn_info(dsn)
if not dsn_info:
critical_error(f"DSN {dsn} not found")
self.cnc = Gda.Connection.open_from_dsn(
dsn_info, None, Gda.ConnectionOptions.NONE)
parser = self.cnc.create_parser()
if not parser:
parser = Gda.sql_parser()
sql = "SELECT id, name, default_served_by FROM customers ORDER BY name"
try:
customers_data_model = self.cnc.execute_select_command(sql)
except Exception as exc:
critical_error("SELECT from customers failed", str(exc))
sql = "SELECT id, name FROM salesrep ORDER BY name"
try:
salesrep_data_model = self.cnc.execute_select_command(sql)
except Exception as exc:
critical_error("SELECT from salesrep failed", str(exc))
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.add(vbox)
# main form to list customers
form = Gdaui.Form(model=customers_data_model)
vbox.pack_start(form, True, True, 0)
# use a combo box to list salesrep by their name and not ID
dm_iter = form.get_data_set()
holder = dm_iter.get_holder_for_field(2)
holder.set_source_model(salesrep_data_model, 0)
button = Gtk.Button(label="Quit")
button.connect("clicked", self.quit)
vbox.add(button)
self.connect("destroy", self.quit)
self.show_all()
def quit(self, widget):
self.cnc.close()
Gtk.main_quit()
SimpleUIForm()
Gtk.main()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment