GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Oct 24, 2014 3:28 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: window does not open correctly once destroyed
PostPosted: Tue May 07, 2013 5:58 am 
Offline
Familiar Face

Joined: Mon May 06, 2013 11:10 am
Posts: 5
I am using gtk+3.4.2 and python 2.7. I wanted to create a main window and a button for another sub window to open. I have put an exit button for quitting the sub-window. Here is the code.

Code:
#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk

class MainWindow(gtk.Window):
   def __init__(self):
      gtk.Window.__init__(self)
   
      #self.box1 = gtk.HBox()
      #self.button1 = gtk.Button("Exit")
      self.set_border_width(10)
      self.button_win = gtk.Button("Window2")
      self.button_win.connect("clicked", self.win2)
      self.set_position(gtk.WIN_POS_CENTER)
      self.set_size_request(600, 400)
      self.set_title("Main Window")
      self.add(self.button_win)

      self.connect("destroy", self.destroy)
      self.show_all()
   
   def win2(self, widget):
      window2.show_all()
   
   def destroy(self, widget):
      print "You clicked Close Button"
      gtk.main_quit()
   
   def main(self):
      gtk.main()

class SubWindow(gtk.Window):
   def __init__(self):
      gtk.Window.__init__(self)
      self.set_position(gtk.WIN_POS_CENTER)
      self.set_size_request(400, 200)
      self.set_title("Sub Window")
      
      self.button2 = gtk.Button("exit")
      self.add(self.button2)
      self.button2.connect("clicked", lambda w:self.destroy())

if __name__ == "__main__":
   window1 = MainWindow()
   window2 = SubWindow()
   window1.main()


It will run just fine at first time, but when I destroy the second window and open it again, It wont show the button "Window2". But all other parameters size and title will be there. I dont want to hide the window. Is there any solution to get that button back?


Top
 Profile  
 
 Post subject: Re: window does not open correctly once destroyed
PostPosted: Thu May 09, 2013 3:55 pm 
Offline
GTK+ Geek

Joined: Fri Sep 04, 2009 6:53 am
Posts: 68
If you destroy a widget, it isn't no longer in any consistent state. It's kaputt. Why don't you just use hide?


Top
 Profile  
 
 Post subject: Re: window does not open correctly once destroyed
PostPosted: Thu May 09, 2013 6:51 pm 
Offline
Familiar Face

Joined: Mon May 06, 2013 11:10 am
Posts: 5
Quote:
Why don't you just use hide?
I could hide it. Say I plan to put a button for print "..". If I hide the window and show it again, and then press the button for printing it prints twice. If I repeat the process again, it prints thrice. That is why I didnt want to hide it.


Top
 Profile  
 
 Post subject: Re: window does not open correctly once destroyed
PostPosted: Fri May 10, 2013 2:40 pm 
Offline
GTK+ Geek

Joined: Fri Sep 04, 2009 6:53 am
Posts: 68
I suspect when you show your window2, you connect the print button to your callback again. If you click, your callback is called twice. If you show again, the callback is connected again. Click again and your callback is called thrice, ....

I have no problem with this:

Code:

#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk

class MainWindow(gtk.Window):
   def __init__(self):
      gtk.Window.__init__(self)
   
      #self.box1 = gtk.HBox()
      #self.button1 = gtk.Button("Exit")
      self.set_border_width(10)
      self.button_win = gtk.Button("Window2")
      self.button_win.connect("clicked", self.win2)
      self.set_position(gtk.WIN_POS_CENTER)
      self.set_size_request(600, 400)
      self.set_title("Main Window")
      self.add(self.button_win)

      self.connect("destroy", self.destroy)
      self.show_all()
   
   def win2(self, widget):
      window2.show_all()
   
   def destroy(self, widget):
      print "You clicked Close Button"
      gtk.main_quit()
   
   def main(self):
      gtk.main()

class SubWindow(gtk.Window):
   def __init__(self):
      gtk.Window.__init__(self)
      self.set_position(gtk.WIN_POS_CENTER)
      self.set_size_request(400, 200)
      self.set_title("Sub Window")
     
      hbox=gtk.VBox()
      self.add(hbox)

      button1=gtk.Button(stock=gtk.STOCK_PRINT)
      hbox.add(button1)
      button1.connect("clicked", self.myprint)
      # button1.connect("clicked", self.myprint) #your callback is called twice!!

      self.button2 = gtk.Button("exit")
      hbox.add(self.button2)
      self.button2.connect("clicked", lambda w:self.hide())

   def myprint(self,widget):
        print "print"

if __name__ == "__main__":
   window1 = MainWindow()
   window2 = SubWindow()
   window1.main()


Top
 Profile  
 
 Post subject: Re: window does not open correctly once destroyed
PostPosted: Sun May 12, 2013 7:14 am 
Offline
Familiar Face

Joined: Mon May 06, 2013 11:10 am
Posts: 5
Yes. That worked fine. Thanks. I was calling callback for printing in 'win2' method itself.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 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