如何在GTK +中设置小部件的位置?

时间:2015-04-02 23:54:43

标签: gtk

请,我需要能够使用像素在gtk +上设置Widget位置的功能!以及它的一个例子。

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用GtkFixed容器:

  

GtkFixed小部件是一个容器,可以将子小部件放置在固定位置并且具有固定大小,以像素为单位。


由于你没有提到一种语言,这里有一个Python的例子:

from gi.repository import Gtk
import sys

class MainWindow(Gtk.ApplicationWindow):

    def __init__(self, app):
        Gtk.Window.__init__(self, title="GtkFixed", application=app)
        # Set the window size
        self.set_size_request(200, 200)

        # Add GtkFixed to the main window
        container = Gtk.Fixed()
        self.add(container)

        button = Gtk.Button("Test Button")
        # Add the button in the (x=20,y=100) position
        container.put(button, 20, 100)


class Application(Gtk.Application):

    def __init__(self):
        Gtk.Application.__init__(self)

    def do_activate(self):
        self.mainWindow = MainWindow(self)
        self.mainWindow.show_all()

    def do_startup(self):
        Gtk.Application.do_startup(self)


application = Application()

exitStatus = application.run(sys.argv)
sys.exit(exitStatus)

(遗憾的是我无法发布结果图片)