Experimenting with Go GTK+
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

115 lines
3.7 KiB

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject, GLib
class Main:
def __init__(self):
glade_file = "ui/app.glade"
self.builder = Gtk.Builder()
self.builder.add_from_file(glade_file)
self.builder.connect_signals(self)
self.mw = self.builder.get_object("main_window")
self.stack = self.builder.get_object("mw_stack")
self.stack_main = self.builder.get_object("box_main")
self.stack_about = self.builder.get_object("box_about")
self.stack_detail = self.builder.get_object("box_detail")
self.detail_header = self.builder.get_object("box_detail_header")
self.dlg_new = self.builder.get_object("dlg_new_file")
self.listbox = self.builder.get_object("mw_list")
self.dyn_btn_box = self.builder.get_object("dyn_btn_box")
for row in ["First Button", "Second", "Third"]:
self.add_button(row)
# Note: the UI size request is 360x720, same resolution as
# the Pinephone screen, but this is too big for the window to
# be. As workaround, we reset the size request very small and maximize
# the window.
self.mw.set_size_request(360, 200)
self.mw.maximize()
self.mw.show_all()
Gtk.main()
def add_button(self, label):
btn = Gtk.Button.new_with_label(label)
btn.connect("clicked", lambda *args: self.goto_detail_screen(label))
self.dyn_btn_box.pack_start(btn, expand=False, fill=True, padding=0)
def goto_detail_screen(self, label):
self.detail_header.set_text("Detail: " + label)
self.stack.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT)
self.stack.set_visible_child(self.stack_detail)
### methods below are Glade signal handlers
def on_main_window_destroy(self, *args):
Gtk.main_quit()
def on_menu_new_activate(self, *args):
print("new clicked")
self.dlg_new.show()
def on_menu_open_activate(self, *args):
print("open clicked")
print(dir(Gtk.FileChooserDialog))
dialog = Gtk.FileChooserDialog(
"Please choose a file",
self.mw,
Gtk.FileChooserAction.OPEN,
(
Gtk.STOCK_CANCEL,
Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN,
Gtk.ResponseType.OK,
),
)
filter = Gtk.FileFilter()
filter.set_name("JPEG images")
filter.add_mime_type("image/jpeg")
dialog.add_filter(filter)
filter_any = Gtk.FileFilter()
filter_any.set_name("Any files")
filter_any.add_pattern("*")
dialog.add_filter(filter_any)
resp = dialog.run()
if resp == Gtk.ResponseType.OK:
print("file chosen:", dialog.get_filename())
elif resp == Gtk.ResponseType.CANCEL:
print("canceled!")
dialog.destroy()
def on_menu_quit_activate(self, *args):
self.mw.destroy()
def on_btn_about_clicked(self, *args):
print("about clicked")
self.stack.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT)
self.stack.set_visible_child(self.stack_about)
def on_btn_about_back_clicked(self, *args):
print("about back")
self.stack.set_transition_type(Gtk.StackTransitionType.SLIDE_RIGHT)
self.stack.set_visible_child(self.stack_main)
def on_btn_detail_back_clicked(self, *args):
self.on_btn_about_back_clicked(*args)
def on_menu_save_activate(self, *args):
pass
def on_menu_home_activate(self, *args):
self.on_btn_about_back_clicked(*args)
def on_new_file_selected(self, *args):
pass
if __name__ == "__main__":
Main()