[kwlug-disc] Question Involving GTK 3 Programming in Python
John Driezen
jdriezen at sympatico.ca
Sat May 1 18:31:18 EDT 2021
Thank you Paul. Your suggestion of making material_combo an instance
variable by prepending self. works, and makes my code much cleaner.
John Driezen
jdriezen at sympatico.ca
On 2021-05-01 2:49 p.m., Paul Nijjar via kwlug-disc wrote
> You prepend 'self.' in front of instance variables:
>
> class ScrapAppWindow(Gtk.Window):
>
> # ... snip ...
>
> 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"])
>
> # ... snip ...
>
> # This will make self.material_combo available in the class, I think
> self.material_combo = Gtk.ComboBox.new_with_model(material_store)
> self.material_combo.connect("changed", self.on_material_combo_changed)
> renderer_text = Gtk.CellRendererText()
> self.material_combo.pack_start(renderer_text, True)
> self.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(self.material_combo, True, True, 0)
> vbox.pack_start(plot_button, True, True, 0)
>
> self.add(vbox)
> self.show_all()
>
> Then your on_plot_graph_clicked() function becomes something like:
>
> def on_plot_graph_clicked(self, button):
> print('"Plot Graph" Button was clicked.')
>
> selected_material = None
>
> tree_iter = self.material_combo.get_active_iter()
> if tree_iter is not None:
> model = self.material_combo.get_model()
> selected_material = model[tree_iter][0]
>
> if (selected_material is None) or (selected_material=="All Materials"):
> 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()
> else:
> print("Calling plotgraph(%s)."� % selected_material)
> plotgraph(selected_material)
>
>
> (I did not test this, and I could be very very wrong, but I believe this is how you set up classes in Python. See: https://www.digitalocean.com/community/tutorials/understanding-class-and-instance-variables-in-python-3
>
> - Paul
>
>> On 2021-04-29 3:41 p.m., Paul Nijjar via kwlug-disc wrote:
>>> You have a class ScrapAppWindow. My (possibly wrong) idea would be to
>>> make material_combo a class variable, rather than a variable that is
>>> local to the init() function. Then it will be accessible from your
>>> other functions.
>>>
>>> - Paul
>>>
>>>
>> Your suggestion is a good one Paul. However I am at a total loss on how to
>> implement your suggestion in Python.
>>
>> I did manage to get the Plot Graph button working by passing the
>> material_combo variable to the on_plot_graph_clicked method. Sample code
>> follows:
>>
>> def on_plot_graph_clicked(self, button, combo):
>> print('"Plot Graph" Button was clicked.')
>>
>> selected_material = None
>>
>> tree_iter = combo.get_active_iter()
>> if tree_iter is not None:
>> model = combo.get_model()
>> selected_material = model[tree_iter][0]
>>
>> if (selected_material is None) or (selected_material=="All Materials"):
>> 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()
>> else:
>> print("Calling plotgraph(%s)." % selected_material)
>> plotgraph(selected_material)
>>
>> I am calling the on_plot_graph_clicked method using this line.
>>
>> plot_button.connect("clicked", self.on_plot_graph_clicked, material_combo)
>>
>> My solution works, but becomes unwieldy very quickly with multiple combo
>> boxes.
>>
>> John Driezen
>> jdriezen at sympatico.ca
>>
>> On 2021-04-29 3:41 p.m., Paul Nijjar via kwlug-disc wrote:
>>> You have a class ScrapAppWindow. My (possibly wrong) idea would be to
>>> make material_combo a class variable, rather than a variable that is
>>> local to the init() function. Then it will be accessible from your
>>> other functions.
>>>
>>> - Paul
>>>
>>>
>> _______________________________________________
>> kwlug-disc mailing list
>> kwlug-disc at kwlug.org
>> https://kwlug.org/mailman/listinfo/kwlug-disc_kwlug.org
More information about the kwlug-disc
mailing list