[kwlug-disc] Question Involving GTK 3 Programming in Python

John Driezen jdriezen at sympatico.ca
Wed Apr 28 21:57:33 EDT 2021


I have run into a snag writing my scrap app program.  Sample code follows.

import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

class ScrapAppWindow(Gtk.Window):
     def on_material_combo_changed(self, combo):
         tree_iter = combo.get_active_iter()
         if tree_iter is not None:
             model = combo.get_model()
             selected_material = model[tree_iter][0]
             print("Selected: material=%s" % selected_material)

     def on_plot_graph_clicked(self, button):
         print('"Plot Graph" Button was clicked.')

         # TODO: Get selected_material from material_combobox
         # and call plotgraph(selected_material)

         #if selected_material is not None:
         #    plotgraph(selected_material)
         #else:

         dialog = Gtk.MessageDialog(
             transient_for=self,
             flags=0,
             message_type=Gtk.MessageType.INFO,
             buttons=Gtk.ButtonsType.OK,
             text="Please select a material.",
         )
         dialog.run()
         print("Plot Graph dialog closed.")
         dialog.destroy()

     def __init__(self):
         Gtk.Window.__init__(self, title="Scrap App")
         self.set_border_width(10)

         material_label = Gtk.Label(label="Please Select Material")

         material_store = Gtk.ListStore(str)

         material_store.append(["#1 Bright Copper"])
         material_store.append(["#1 Copper"])
         material_store.append(["#2 Copper"])
         material_store.append(["#3 Copper"])
         material_store.append(["Aluminum Extrusion"])
         material_store.append(["Tin"])
         material_store.append(["Steel"])
         material_store.append(["Aluminum Cast"])
         material_store.append(["Yellow Brass"])
         material_store.append(["Die Cast Zinc"])

         material_combo = Gtk.ComboBox.new_with_model(material_store)
         material_combo.connect("changed", self.on_material_combo_changed)
         renderer_text = Gtk.CellRendererText()
         material_combo.pack_start(renderer_text, True)
         material_combo.add_attribute(renderer_text, "text", 0)

         plot_button = Gtk.Button.new_with_label("Plot Graph")
         plot_button.connect("clicked", self.on_plot_graph_clicked)

         vbox = Gtk.Box(orientation = Gtk.Orientation.VERTICAL, spacing=10)
         vbox.pack_start(material_label, True, True, 0)
         vbox.pack_start(material_combo, True, True, 0)
         vbox.pack_start(plot_button, True, True, 0)

         self.add(vbox)
         self.show_all()

win = ScrapAppWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

My question is how do I retrieve the material combobox value from within 
the on_plot_graph_clicked method?  For brevity, the plotgraph routine is 
not included.

John Driezen

jdriezen at sympatico.ca





More information about the kwlug-disc mailing list