1

Is there a way to change direction of Gtk.Window in sample code below and make it Right to Left? i tried to change it with gravity but didn't worked.

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()
  • You are trying to do what? – theGtknerd Jan 10 '18 at 17:23
  • in Left to Right window when you add a widget (like button) to window, button will placed in top left corner (top left corner(0, 0)) will be refrence point. i want to start from right top corner. i hope i explaned. –  Jan 10 '18 at 18:36

1 Answers1

0

Try this:

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()

By the way, you can easily figure stuff like this out by using Glade to create a mockup.

Edit: according to this answer, you would change the direction. It didn't work for me, but maybe my language settings have something to do with it.

See also set_direction.

theGtknerd
  • 3,647
  • 1
  • 13
  • 34
  • grid.set_halign(Gtk.Align.END) doesn't make any difference. it is hard to believe that GTK dosn't have any feature for right to left layout. for people how has right to left language like arabic or persian –  Jan 11 '18 at 08:07
  • you know, in right to left layout every thing start form right side of window even Window Title. and right side of window would be start point (0, 0) –  Jan 11 '18 at 08:12
  • thanks for helping me but python gave me this error: Traceback (most recent call last): File "SimpleWindow.py", line 51, in win = MainWindow() File "SimpleWindow.py", line 18, in __init__ gtk_widget_set_direction(grid, GTK_TEXT_DIR_RTL) NameError: name 'gtk_widget_set_direction' is not defined –  Jan 11 '18 at 21:44
  • Although what you suggested works perfectly in GTK+ C, but not PyGObject –  Jan 11 '18 at 22:20
  • I guess you realize `gtk_widget_set_direction(grid, GTK_TEXT_DIR_RTL)` is C. Python is `grid.set_direction(Gtk.TextDirection.RTL)`. Why this wouldn't work in Python but works in C baffles me. Could you post your C code for me to try out? I have almost zero experience in C. – theGtknerd Jan 11 '18 at 23:22
  • `grid.set_direction(Gtk.TextDirection.RTL)` Works. i think i used c code for python [sad face]. could you tell me where did you found this? because when i search for gtk reference or pygobject reference all i get was c GTK. Thankyou for helping me. –  Jan 12 '18 at 09:59
  • There is no really good Python reference for Gtk. Have a look [here](https://stackoverflow.com/questions/8420108/is-there-a-gi-repository-documentation-for-python). I just learned how to port the C to Python. – theGtknerd Jan 12 '18 at 12:22