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.

116 lines
3.7 KiB

  1. import gi
  2. gi.require_version('Gtk', '3.0')
  3. from gi.repository import Gtk, GObject, GLib
  4. class Main:
  5. def __init__(self):
  6. glade_file = "ui/app.glade"
  7. self.builder = Gtk.Builder()
  8. self.builder.add_from_file(glade_file)
  9. self.builder.connect_signals(self)
  10. self.mw = self.builder.get_object("main_window")
  11. self.stack = self.builder.get_object("mw_stack")
  12. self.stack_main = self.builder.get_object("box_main")
  13. self.stack_about = self.builder.get_object("box_about")
  14. self.stack_detail = self.builder.get_object("box_detail")
  15. self.detail_header = self.builder.get_object("box_detail_header")
  16. self.dlg_new = self.builder.get_object("dlg_new_file")
  17. self.listbox = self.builder.get_object("mw_list")
  18. self.dyn_btn_box = self.builder.get_object("dyn_btn_box")
  19. for row in ["First Button", "Second", "Third"]:
  20. self.add_button(row)
  21. # Note: the UI size request is 360x720, same resolution as
  22. # the Pinephone screen, but this is too big for the window to
  23. # be. As workaround, we reset the size request very small and maximize
  24. # the window.
  25. self.mw.set_size_request(360, 200)
  26. self.mw.maximize()
  27. self.mw.show_all()
  28. Gtk.main()
  29. def add_button(self, label):
  30. btn = Gtk.Button.new_with_label(label)
  31. btn.connect("clicked", lambda *args: self.goto_detail_screen(label))
  32. self.dyn_btn_box.pack_start(btn, expand=False, fill=True, padding=0)
  33. def goto_detail_screen(self, label):
  34. self.detail_header.set_text("Detail: " + label)
  35. self.stack.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT)
  36. self.stack.set_visible_child(self.stack_detail)
  37. ### methods below are Glade signal handlers
  38. def on_main_window_destroy(self, *args):
  39. Gtk.main_quit()
  40. def on_menu_new_activate(self, *args):
  41. print("new clicked")
  42. self.dlg_new.show()
  43. def on_menu_open_activate(self, *args):
  44. print("open clicked")
  45. print(dir(Gtk.FileChooserDialog))
  46. dialog = Gtk.FileChooserDialog(
  47. "Please choose a file",
  48. self.mw,
  49. Gtk.FileChooserAction.OPEN,
  50. (
  51. Gtk.STOCK_CANCEL,
  52. Gtk.ResponseType.CANCEL,
  53. Gtk.STOCK_OPEN,
  54. Gtk.ResponseType.OK,
  55. ),
  56. )
  57. filter = Gtk.FileFilter()
  58. filter.set_name("JPEG images")
  59. filter.add_mime_type("image/jpeg")
  60. dialog.add_filter(filter)
  61. filter_any = Gtk.FileFilter()
  62. filter_any.set_name("Any files")
  63. filter_any.add_pattern("*")
  64. dialog.add_filter(filter_any)
  65. resp = dialog.run()
  66. if resp == Gtk.ResponseType.OK:
  67. print("file chosen:", dialog.get_filename())
  68. elif resp == Gtk.ResponseType.CANCEL:
  69. print("canceled!")
  70. dialog.destroy()
  71. def on_menu_quit_activate(self, *args):
  72. self.mw.destroy()
  73. def on_btn_about_clicked(self, *args):
  74. print("about clicked")
  75. self.stack.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT)
  76. self.stack.set_visible_child(self.stack_about)
  77. def on_btn_about_back_clicked(self, *args):
  78. print("about back")
  79. self.stack.set_transition_type(Gtk.StackTransitionType.SLIDE_RIGHT)
  80. self.stack.set_visible_child(self.stack_main)
  81. def on_btn_detail_back_clicked(self, *args):
  82. self.on_btn_about_back_clicked(*args)
  83. def on_menu_save_activate(self, *args):
  84. pass
  85. def on_menu_home_activate(self, *args):
  86. self.on_btn_about_back_clicked(*args)
  87. def on_new_file_selected(self, *args):
  88. pass
  89. if __name__ == "__main__":
  90. Main()