GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Dec 22, 2014 7:47 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Problem closing dialogs when wanted...
PostPosted: Tue Dec 25, 2007 11:10 pm 
Offline

Joined: Tue Dec 25, 2007 8:04 pm
Posts: 3
Hi all!
I've just started to try to learn Python and creating GUI using GTK/glade. Since I'm completely newbie to Python/GTK, I need som help to get "started". I have an example program consisting of 3 python-files and 1 glade-file. The sample-program starts a welcome-dialog, followed by a window for the main-program.

What I want: When I click the "Close"-button in the welcome-dialog, I want the dialog to be closed, and then the window for the main-program should appear.

What happens: When I click the "Close"-button in the welcome-dialog, the window for the main-program opens, but the welcome-dialog does not close.

What do I do wrong here?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 25, 2007 11:11 pm 
Offline

Joined: Tue Dec 25, 2007 8:04 pm
Posts: 3
...and here are the codes:

program.py
Code:
#!/usr/bin/env python
#
# Import Python native system classes
import sys
import os

try:
    import pygtk
    pygtk.require('2.0')
except ImportError:
    pass
#
import gtk.gdk
import gtk
import gtk.glade
import gobject
import welcome
import mainprogram
#
args = sys.argv
#
def run():
    try:
        # Display Welcome-dialog
        welcome.welcome()
        gtk.main_quit()
        #
        # Welcome-dialog completed.  Display main-window
        mainprogram.mainprogram()
        # Everything completed -> Quit program
        gtk.main_quit()
    except:
        # Something irregular happened...
        print "Error from program..."

gobject.timeout_add(100, run, priority=100)
gtk.main()





welcome.py
Code:
import os
import gtk

class welcome():
    def __init__(self):
        print "Welcome-dialog starts..."
        #
        # Prepare window and assign title
        xml = gtk.glade.XML('./prog.glade', "dlgWelcome", "dlgWelcome")
        self.window = xml.get_widget('dlgWelcome')
        self.window.set_title('Welcome to testprogram...')
        #
        # Style and labels for different text in the window
        self.btnClose = xml.get_widget('btnClose')
        self.btnClose.connect("clicked", self.destroy)
        #
        # Display the stuff
        self.window.show_all()
        gtk.main()

    def destroy(self, widget):
        print "Closing Welcome-dialog..."
        gtk.main_quit()



mainprogram.py
Code:
import os
import gtk

class mainprogram:
    def destroy(self, widget):
        print "Closing Mainprogram-dialog..."
        gtk.main_quit()

    def __init__(self):
        print "Mainprogram dialog starts..."
        #
        # Prepare window and assign title
        xml = gtk.glade.XML('./prog.glade', "winMainProgram", "winMainProgram")
        self.window = xml.get_widget('winMainProgram')
        #
        # Display the stuff
        self.window.show_all()
        gtk.main()



prog.glade
Code:
<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->

<glade-interface>

<widget class="GtkWindow" id="winMainProgram">
  <property name="visible">True</property>
  <property name="title" translatable="yes">Main-program</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>
  <property name="decorated">True</property>
  <property name="skip_taskbar_hint">False</property>
  <property name="skip_pager_hint">False</property>
  <property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
  <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
  <property name="focus_on_map">True</property>
  <property name="urgency_hint">False</property>

  <child>
    <widget class="GtkVBox" id="vbox2">
      <property name="visible">True</property>
      <property name="homogeneous">False</property>
      <property name="spacing">0</property>

      <child>
   <widget class="GtkLabel" id="label1">
     <property name="visible">True</property>
     <property name="label" translatable="yes">Main program...</property>
     <property name="use_underline">False</property>
     <property name="use_markup">False</property>
     <property name="justify">GTK_JUSTIFY_LEFT</property>
     <property name="wrap">False</property>
     <property name="selectable">False</property>
     <property name="xalign">0.5</property>
     <property name="yalign">0.5</property>
     <property name="xpad">0</property>
     <property name="ypad">0</property>
     <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
     <property name="width_chars">-1</property>
     <property name="single_line_mode">False</property>
     <property name="angle">0</property>
   </widget>
   <packing>
     <property name="padding">0</property>
     <property name="expand">False</property>
     <property name="fill">False</property>
   </packing>
      </child>

      <child>
   <widget class="GtkLabel" id="label2">
     <property name="visible">True</property>
     <property name="label" translatable="yes">Some text...</property>
     <property name="use_underline">False</property>
     <property name="use_markup">False</property>
     <property name="justify">GTK_JUSTIFY_LEFT</property>
     <property name="wrap">False</property>
     <property name="selectable">False</property>
     <property name="xalign">0.5</property>
     <property name="yalign">0.5</property>
     <property name="xpad">0</property>
     <property name="ypad">50</property>
     <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
     <property name="width_chars">-1</property>
     <property name="single_line_mode">False</property>
     <property name="angle">0</property>
   </widget>
   <packing>
     <property name="padding">0</property>
     <property name="expand">False</property>
     <property name="fill">False</property>
   </packing>
      </child>

      <child>
   <widget class="GtkHButtonBox" id="hbuttonbox2">
     <property name="visible">True</property>
     <property name="layout_style">GTK_BUTTONBOX_SPREAD</property>
     <property name="spacing">0</property>

     <child>
       <widget class="GtkButton" id="button1">
         <property name="visible">True</property>
         <property name="can_default">True</property>
         <property name="can_focus">True</property>
         <property name="label">gtk-apply</property>
         <property name="use_stock">True</property>
         <property name="relief">GTK_RELIEF_NORMAL</property>
         <property name="focus_on_click">True</property>
       </widget>
     </child>

     <child>
       <widget class="GtkButton" id="button2">
         <property name="visible">True</property>
         <property name="can_default">True</property>
         <property name="can_focus">True</property>
         <property name="label">gtk-quit</property>
         <property name="use_stock">True</property>
         <property name="relief">GTK_RELIEF_NORMAL</property>
         <property name="focus_on_click">True</property>
       </widget>
     </child>
   </widget>
   <packing>
     <property name="padding">0</property>
     <property name="expand">True</property>
     <property name="fill">True</property>
   </packing>
      </child>
    </widget>
  </child>
</widget>

<widget class="GtkDialog" id="dlgWelcome">
  <property name="visible">True</property>
  <property name="title" translatable="yes">dialog2</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>
  <property name="decorated">True</property>
  <property name="skip_taskbar_hint">False</property>
  <property name="skip_pager_hint">False</property>
  <property name="type_hint">GDK_WINDOW_TYPE_HINT_DIALOG</property>
  <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
  <property name="focus_on_map">True</property>
  <property name="urgency_hint">False</property>
  <property name="has_separator">True</property>

  <child internal-child="vbox">
    <widget class="GtkVBox" id="dialog-vbox1">
      <property name="visible">True</property>
      <property name="homogeneous">False</property>
      <property name="spacing">0</property>

      <child internal-child="action_area">
   <widget class="GtkHButtonBox" id="dialog-action_area1">
     <property name="visible">True</property>
     <property name="layout_style">GTK_BUTTONBOX_END</property>

     <child>
       <widget class="GtkButton" id="btnClose">
         <property name="visible">True</property>
         <property name="can_default">True</property>
         <property name="can_focus">True</property>
         <property name="label">gtk-close</property>
         <property name="use_stock">True</property>
         <property name="relief">GTK_RELIEF_NORMAL</property>
         <property name="focus_on_click">True</property>
         <property name="response_id">-7</property>
       </widget>
     </child>
   </widget>
   <packing>
     <property name="padding">0</property>
     <property name="expand">False</property>
     <property name="fill">True</property>
     <property name="pack_type">GTK_PACK_END</property>
   </packing>
      </child>

      <child>
   <widget class="GtkLabel" id="lblURL">
     <property name="visible">True</property>
     <property name="label" translatable="yes">Welcome to the program</property>
     <property name="use_underline">False</property>
     <property name="use_markup">False</property>
     <property name="justify">GTK_JUSTIFY_LEFT</property>
     <property name="wrap">False</property>
     <property name="selectable">False</property>
     <property name="xalign">0.5</property>
     <property name="yalign">0.5</property>
     <property name="xpad">0</property>
     <property name="ypad">0</property>
     <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
     <property name="width_chars">-1</property>
     <property name="single_line_mode">False</property>
     <property name="angle">0</property>
   </widget>
   <packing>
     <property name="padding">0</property>
     <property name="expand">False</property>
     <property name="fill">False</property>
   </packing>
      </child>
    </widget>
  </child>
</widget>

</glade-interface>


Any tips to help me getting any further is highly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 26, 2007 5:38 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
I've never used python, but tinkering with your code for a couple minutes, I see a few things. You only need one call to gtk.main during your program--not for each window. You can use the run() method of GtkDialog to display your welcome screen modally. You need to call gtk.main_quit() only when you're ready for you application to end.

So, try these modified files:

Program.py
[code=]#!/usr/bin/env python
#
# Import Python native system classes
import sys
import os

try:
import pygtk
pygtk.require('2.0')
except ImportError:
pass
#
import gtk.gdk
import gtk
import gtk.glade
import gobject
import welcome
import mainprogram
#
args = sys.argv
#
def run():
try:
# Display Welcome-dialog (will pause here until dialog is closed)
welcome.welcome()

#
# Welcome-dialog completed. Display main-window
mainprogram.mainprogram()

gtk.main()

except:
# Something irregular happened...
print "Error from program..."

run()[/code]

welcome.py[code=]import os
import gtk

class welcome():
def __init__(self):
print "Welcome-dialog starts..."
#
# Prepare window and assign title
xml = gtk.glade.XML('./prog.glade', "dlgWelcome", "dlgWelcome")
self.window = xml.get_widget('dlgWelcome')
self.window.set_title('Welcome to testprogram...')
#
# Style and labels for different text in the window
self.btnClose = xml.get_widget('btnClose')
self.btnClose.connect("clicked", self.destroy)
#
# Use the run() from GtkDialog so that we wait for the dialog to be
# closed before continuing...
self.window.run()


def destroy(self, widget):
print "Closing Welcome-dialog..."
self.window.destroy()
[/code]

mainprogram.py
[code=]import os
import gtk

class mainprogram:
def destroy(self, widget):
print "Closing Mainprogram-dialog..."
gtk.main_quit()

def __init__(self):
print "Mainprogram dialog starts..."
#
# Prepare window and assign title
xml = gtk.glade.XML('./prog.glade', "winMainProgram", "winMainProgram")
self.window = xml.get_widget('winMainProgram')

# must be able to terminate program from both 'x' in title bar and
# the quit button
self.window.connect("destroy", self.destroy)
self.btnQuit = xml.get_widget('button2')
self.btnQuit.connect("clicked", self.destroy)

#
# Display the stuff
self.window.show_all()
[/code]

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 26, 2007 11:02 pm 
Offline

Joined: Tue Dec 25, 2007 8:04 pm
Posts: 3
Micah Carrick: Thank you very much for your help and tips! It worked just as intended :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group