Python GTK+ 3 从右到左

Right to Left in Python GTK+ 3

有没有办法改变下面示例代码中 Gtk.Window 的方向,使其从右到左?我试图用重力改变它但没有奏效。

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk

class MainWindow(Gtk.Window):

    def __init__(self):

        Gtk.Window.__init__(self, title="DEMO")
        self.set_position(Gtk.WindowPosition.CENTER)
        self.set_gravity(Gdk.Gravity.NORTH_EAST)

        grid = Gtk.Grid()
        self.add(grid)

        button1 = Gtk.Button(label="Button 1")
        button2 = Gtk.Button(label="Button 2")
        button3 = Gtk.Button(label="Button 3")
        button4 = Gtk.Button(label="Button 4")
        button5 = Gtk.Button(label="Button 5")
        button6 = Gtk.Button(label="Button 6")

        grid.add(button1)
        grid.attach(button2, 1, 0, 2, 1)
        grid.attach_next_to(button3, button1, Gtk.PositionType.BOTTOM, 1, 2)
        grid.attach_next_to(button4, button3, Gtk.PositionType.RIGHT, 2, 1)
        grid.attach(button5, 1, 2, 1, 1)
        grid.attach_next_to(button6, button5, Gtk.PositionType.RIGHT, 1, 1)

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

试试这个:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk

class MainWindow(Gtk.Window):

    def __init__(self):

        Gtk.Window.__init__(self, title="DEMO")
        self.set_position(Gtk.WindowPosition.CENTER)
        self.set_gravity(Gdk.Gravity.NORTH_EAST)
        self.set_direction(Gtk.TextDirection.RTL)
        grid = Gtk.Grid()
        grid.set_halign(Gtk.Align.END)
        grid.set_direction(Gtk.TextDirection.RTL)
        self.add(grid)
        button1 = Gtk.Label(label="Button 1")
        button1.set_halign(Gtk.Align.START)
        button1.set_direction(Gtk.TextDirection.RTL)
        button2 = Gtk.Button(label="Button 2")
        button3 = Gtk.Button(label="Button 3")
        button4 = Gtk.Button(label="Button 4")
        button5 = Gtk.Button(label="Button 5")
        button6 = Gtk.Button(label="Button 6")

        grid.add(button1)
        grid.attach(button2, 1, 0, 2, 1)
        grid.attach_next_to(button3, button1, Gtk.PositionType.BOTTOM, 1, 2)
        grid.attach_next_to(button4, button3, Gtk.PositionType.RIGHT, 2, 1)
        grid.attach(button5, 1, 2, 1, 1)
        grid.attach_next_to(button6, button5, Gtk.PositionType.RIGHT, 1, 1)

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

顺便说一下,您可以使用 Glade 创建模型来轻松解决此类问题。

编辑:根据这个answer,你会改变方向。它对我不起作用,但也许我的语言设置与它有关。

另见 set_direction