LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   GTK+ / Python spawn child window button press (https://www.linuxquestions.org/questions/programming-9/gtk-python-spawn-child-window-button-press-4175452638/)

jokerdude 03-04-2013 07:35 AM

GTK+ / Python spawn child window button press
 
I have been searching the web for 4 days now and I am unable to find an answer to my question. I've scoured the gnome website, but their GTK 3 documentation needs work. To me, it looks like a wall of text and is more of a command reference than actual useful documentation for someone who is learning. Anyways, end of rant.

I'm trying to write a simple program using GTK and Python. It should open a main window with a button. When the button, it should spawn a child window with a label. I cannot for the life of me get it to work. Could someone please help me figure out what I'm doing wrong??

Code:

#!/usr/bin/python
from gi.repository import Gtk

class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Main Window")
        self.set_default_size(500,300)
        self.set_border_width(30)
        test_button = Gtk.Button(label="Click Here")
        test_button.connect("clicked", self.on_button_click)
        grid = Gtk.Grid()
        grid.attach(test_button, 1, 1, 1, 1)
        self.add(grid)
    def on_button_click(self, widget):
        ButtonWindow()
       
class ButtonWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Child Window")
        self.set_default_size(500,300)
        self.set_border_width(30)
        child_win.connect("destroy", self.destroy)
        grid = Gtk.Grid()
        test_label = Gtk.Label(label="Label on child window")
        grid.attach(test_label, 1, 1, 1, 1)

main_win = MainWindow()
child_win = ButtonWindow()
main_win.connect("delete-event", Gtk.main_quit)
main_win.show_all()
Gtk.main()

This code does not work. The main window spawns with a button, but clicking it does not spawn the child window. I have seen some programs that spawn a child window, but all of them close it with Gtk.main_quit, which then closes the entire program. Is GTK meant to only have 1 main window with no external windows?

pgpython 03-05-2013 07:21 AM

You have created the window object but you need to call show_all on the new window for it to appear as the first time you called it the window has not been created yet. If it had and was a child of MainWindow then it would show as well


All times are GMT -5. The time now is 09:14 AM.