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 回答