PSALM - demo2.py
Not logged in
#!/usr/bin/env python3
#
# Demonstration on how to create a GTK GUI using Psalm with Python 3.10.6.
#
# Requires Psalm 1.0.8 or higher.
#
# Run as follows:
#   ./demo2.py
#
# December 20, 2022 by Peter van Eerten.
#------------------------------------------------------------------------------------

from subprocess import Popen, PIPE

# Start Psalm
pipe = Popen("./psalm", shell=True, bufsize=1, stdin=PIPE, stdout=PIPE, text=True, close_fds=True)

# Define the pipes
(pin, pout) = (pipe.stdin, pipe.stdout)

# The communication function
def comm(string):
    pin.write(string + '\n')
    pin.flush()
    return pout.readline().strip()

# Send definitions first
comm("DEF libgtk-3.so.0 void gtk_init int int")
comm("DEF libgtk-3.so.0 long gtk_window_new int")
comm("DEF libgtk-3.so.0 void gtk_window_set_title int char*")
comm("DEF libgtk-3.so.0 long gtk_button_new_with_label char*")
comm("DEF libgtk-3.so.0 long gtk_table_new int int int")
comm("DEF libgtk-3.so.0 void gtk_table_attach_defaults long long int int int int")
comm("DEF libgtk-3.so.0 long gtk_entry_new void")
comm("DEF libgtk-3.so.0 char* gtk_entry_get_text long")
comm("DEF libgtk-3.so.0 void gtk_entry_set_text long char*")
comm("DEF libgtk-3.so.0 long gtk_radio_button_new_with_label_from_widget long char*")
comm("DEF libgtk-3.so.0 void gtk_main_iteration void")
comm("DEF libgtk-3.so.0 void gtk_widget_show_all int")
comm("DEF libgtk-3.so.0 void gtk_widget_set_size_request long int int")
comm("DEF libgtk-3.so.0 void gtk_widget_grab_focus long")
comm("DEF libgtk-3.so.0 void gtk_container_add long long")
comm("DEF libgobject-2.0.so.0 long g_signal_connect_data int char* address void* void* int")

# Build GUI
comm("EXE gtk_init NULL NULL")
win = comm("EXE gtk_window_new 0")
comm("EXE gtk_window_set_title " + win + " \"Python Demo program with STDIN\"")
comm("EXE gtk_widget_set_size_request " + win + " 450 400")
table = comm("EXE gtk_table_new 50 50 1")
comm("EXE gtk_container_add " + win + " " + table)
button = comm("EXE gtk_button_new_with_label Exit")
comm("EXE gtk_table_attach_defaults " + table + " " + button + " 41 49 45 49")
entry = comm("EXE gtk_entry_new")
comm("EXE gtk_table_attach_defaults " + table + " " + entry + " 1 40 45 49")
text = comm("EXE gtk_entry_new")
comm("EXE gtk_table_attach_defaults " + table + " " + text + " 1 49 8 12")
radio1 = comm("EXE gtk_radio_button_new_with_label_from_widget NULL Yes")
comm("EXE gtk_table_attach_defaults " + table + " " + radio1 + " 1 10 1 4")
radio2 = comm("EXE gtk_radio_button_new_with_label_from_widget " + radio1 + " No")
comm("EXE gtk_table_attach_defaults " + table + " " + radio2 + " 1 10 4 7")
comm("EXE gtk_widget_show_all " + win)
comm("EXE gtk_widget_grab_focus " + entry)

# Connect signals
comm("EXE g_signal_connect_data " + win + " delete-event callback NULL NULL 0")
comm("EXE g_signal_connect_data " + button + " clicked callback NULL NULL 0")
comm("EXE g_signal_connect_data " + entry + " activate callback NULL NULL 0")

# Initialize
event = 0

# Mainloop
while event != button and event != win:
    comm("EXE gtk_main_iteration")
    event = comm("CALLBACK 1")
    if event == entry:
        tmp = comm("EXE gtk_entry_get_text " + entry)
        comm("EXE gtk_entry_set_text " + text + " " + tmp)

# Exit Psalm
comm("EXIT")

Return to PSALM