0

ボタンの再描画が失敗するのはなぜですか?最初にボタンがある場所で起動し、次に上部のウィンドウのサイズを低くしてから、ボタンを別のサイズに縮小したいのですが、buttononTop()メソッドからのボタンの再描画が失敗します。

#!/usr/bin/python
import pygtk, gtk, gobject

class GTK_Main:

  def __init__(self):

    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.window.set_default_size(800, 768)
    self.window.connect("destroy", gtk.main_quit, "WM destroy")
    self.vbox = gtk.VBox()
    self.window.add(self.vbox)
    self.hbox = gtk.HBox(False, 0)
    self.vbox.pack_start(self.hbox, False)    
    self.hbox.set_border_width(10)
    self.hbox.pack_start(gtk.Label(), True, True, 0)
    self.button2 = gtk.Button("Quit")
    self.button2.connect("clicked", self.exit)    
    self.hbox.pack_start(self.button2, False)
    self.button3 = gtk.Button("Test")
    self.button3.connect("clicked", self.buttononTop)    
    self.hbox.pack_start(self.button3, False)    
    self.hbox.add(gtk.Label())
    self.window.show_all()

  def buttononTop(self, w):
    print "Buttons on top - redraw with different shapes includeing the window"
    self.window.remove(self.vbox)
    self.window.resize(800, 330)
    self.window.set_size_request(800, 330)
    # ------------------------------------------- [FAILS STARTS]
    self.vbox = gtk.VBox()
    self.window.add(self.vbox)
    self.hbox = gtk.HBox(False, 0)
    self.vbox.pack_start(self.hbox, False)    
    self.hbox.set_border_width(10)
    self.hbox.pack_start(gtk.Label(), True, True, 0)
    self.button2 = gtk.Button("Quit")
    self.button2.connect("clicked", self.exit)    
    self.hbox.pack_start(self.button2, False)
    self.button3 = gtk.Button("Test")
    self.button3.connect("clicked", self.buttononTop)    
    self.hbox.pack_start(self.button3, False)    
    self.hbox.add(gtk.Label())
    # ------------------------------------------- [FAILS END]

  def exit(self, widget, data=None):
    gtk.main_quit()

GTK_Main()
gtk.gdk.threads_init()
gtk.main()
4

1 に答える 1

0

@another.anon.coward のおかげで、彼のフォローアップに従って動作するようになりました。

ここに画像の説明を入力 ここに画像の説明を入力

コード:

#!/usr/bin/python
import pygtk, gtk, gobject

class GTK_Main:

  def __init__(self):

    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

    # if i use set_size_request, the first launch is not running
    # in correct screen (while using dual screen)
    #self.window.set_size_request(1280, 720)
    # therefore by using default size it fix it
    self.window.set_default_size(1280, 720)

    self.window.connect("destroy", gtk.main_quit, "WM destroy")

    self.vbox = gtk.VBox()
    self.window.add(self.vbox)
    self.hbox = gtk.HBox(False, 0)
    self.vbox.pack_start(self.hbox, False)    
    self.hbox.set_border_width(10)
    self.hbox.pack_start(gtk.Label(), True, True, 0)
    self.button2 = gtk.Button("Quit")
    self.button2.connect("clicked", self.exit)    
    self.hbox.pack_start(self.button2, False)
    self.button3 = gtk.Button("Test")
    self.button3.connect("clicked", self.buttononTop)    
    self.hbox.pack_start(self.button3, False)    
    self.hbox.add(gtk.Label())
    self.window.show_all()
    # another.anon.coward: mentioned a great way solve it 
    if(self.window.get_window().get_state() == gtk.gdk.WINDOW_STATE_MAXIMIZED):
      print "unmaximize"
      self.window.unmaximize()

    self.window.move(0,0)

  def buttononTop(self, w):
    print "Buttons on top - redraw with different shapes includeing the window"
    self.window.remove(self.vbox)
    self.window.resize(1280, 100)
    # if i use set_size_request, the first launch is not running
    # in correct screen (while using dual screen)
    #self.window.set_size_request(1280, 720)
    # therefore by using default size it fix it
    self.window.set_default_size(1280, 100)

    # another.anon.coward: mentioned a great way solve it 
    if(self.window.get_window().get_state() == gtk.gdk.WINDOW_STATE_MAXIMIZED):
      print "unmaximize"
      self.window.unmaximize()

    self.window.move(0,0)
    self.window.show_all()

    # ------------------------------------------- [FAILS STARTS]
    self.vbox = gtk.VBox()
    self.window.add(self.vbox)
    self.hbox = gtk.HBox(False, 0)
    self.vbox.pack_start(self.hbox, False)    
    self.hbox.set_border_width(10)
    self.hbox.pack_start(gtk.Label(), True, True, 0)
    self.button2 = gtk.Button("Quit")
    self.button2.connect("clicked", self.exit)    
    self.hbox.pack_start(self.button2, False)
    self.button3 = gtk.Button("Test")
    self.button3.connect("clicked", self.buttononTop)    
    self.hbox.pack_start(self.button3, False)    
    self.hbox.add(gtk.Label())    
    # ------------------------------------------- [FAILS FIXED]    
    self.vbox.show_all()


  def exit(self, widget, data=None):
    gtk.main_quit()

GTK_Main()
gtk.gdk.threads_init()
gtk.main()
于 2012-12-06T14:49:49.797 に答える