Hi, I am new to coding, so please be patient. I know a little bit of C, Matlab and Python (used only in terminal to create some terminal programs) and I would like to contribute by creating an application with a GUI. I read a bit but I don’t understand how does everything works
My hard question is: how can I change elements in the window, when a button is pressed? Everything returned by the functions don’t trigger changes. See this example:
Example
#!/usr/bin/python
# -*- coding: utf-8 -*-
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Handy', '0.0')
from gi.repository import Gtk, GdkPixbuf, Handy
class Program(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Test")
self.set_default_size(800, 600)
self.set_position(Gtk.WindowPosition.CENTER)
hb = Handy.HeaderBar()
hb.set_show_close_button(True)
hb.props.title = "Test"
self.set_titlebar(hb)
test1 = ""
test2 = ""
button1 = Gtk.Button()
button1.add(Gtk.Image.new_from_icon_name(
"document-new", Gtk.IconSize.BUTTON))
button1.connect("clicked", self.on_new_clicked)
hb.pack_start(button1)
button2 = Gtk.Button()
button2.set_label("Press")
button2.connect("clicked", self.on_button_clicked)
self.add(button2)
if (test1 != ""):
label1 = Gtk.Label()
label1.set_text(test1)
self.add(label1)
if (test2 != ""):
label2 = Gtk.Label()
label2.set_text(test2)
self.add(label2)
def on_new_clicked(self, args):
print("You pressed New button: it should prompt something in the main window, but it doesn't")
test1 = "You pressed New button and everything works"
return test1
def on_button_clicked(self, args):
print("You pressed a button in the window: it should prompt something in the main window, but it doesn't")
test2 = "You pressed a button in the window and everything works"
return test2
def main():
app = Program()
app.connect("delete-event", Gtk.main_quit)
app.show_all()
Gtk.main()
if __name__ == '__main__':
main()
This program creates a window with two buttons (“New” in the headerbar and one centered in the main window) and I’d like that if you press one of those a label appears. How can I do that?
This stupid example would help me in solving other complex situations.
Thank you very much!