Search code examples
pythonuser-interfacegtkgtk3pygtk

How do I scale this Gtk3 dialog box up by a constant factor?


I have this code for creating my own password dialog in Gtk. I need to scale it up by 2-3 times for the display it's going to be used on. How do I do this?

#!/usr/bin/env python3

import gi
import sys


gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib


class EntryDialog(Gtk.Dialog):
    def __init__(self, labeltext=None):
        super().__init__(title="Password", modal=True, focus_on_map=True)
        content = self.get_content_area()
        self.timeout_id = None
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,
                       spacing=15, margin=30)
        content.add(vbox)

        if labeltext is None:
            labeltext = "Please enter a password:"
        label = Gtk.Label(labeltext)
        vbox.pack_start(label, True, True, 0)

        hbox = Gtk.Box(spacing=1)
        self.entry = Gtk.Entry()
        self.entry.set_text("")
        self.entry.set_max_length(256)
        self.entry.set_invisible_char('•')
        self.entry.set_visibility(False)
        hbox.pack_start(self.entry, True, True, 0)
        self.show = Gtk.ToggleButton(label="show")
        self.show.set_active(False)
        hbox.pack_start(self.show, True, True, 0)
        vbox.pack_start(hbox, True, True, 0)

        #self.entry.connect("activate", lambda x: print("Enter"))
        self.show.connect("toggled", self.on_show_toggled)
        self.add_buttons(
            Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
            Gtk.STOCK_OK, Gtk.ResponseType.OK
        )
        self.set_default_response(Gtk.ResponseType.OK)
        self.entry.connect("activate", self.on_entry_enter)

    def on_show_toggled(self, button):
        active = button.get_active()
        self.entry.set_visibility(active)
        button.set_label("hide" if active else "show")

    def on_entry_enter(self, button):
        self.activate_default()

    def get_password(self):
        return self.entry.get_text()


def run_dialog(argv):
    if len(argv) == 1:
        win = EntryDialog()
    elif len(argv) == 2:
        win = EntryDialog(argv[1])
    else:
        print(f"Usage: {argv[0]} [<prompt text>]", file=sys.stderr)
        sys.exit(2)
    win.show_all()
    result = win.run()
    if result == Gtk.ResponseType.OK:
        print(win.get_password())
    else:
        sys.exit(1)


if __name__ == '__main__':
    run_dialog(sys.argv)

Solution

  • Using the GDK_SCALE environment variable was suggested on IRC. This works for my use-case, but it seems a poor solution for general use. In my case, this is what I had to change my program to this:

    #!/usr/bin/env python3
    
    import sys
    import os
    
    if __name__ == '__main__':
        scale = os.environ.get('GDK_SCALE', '1')
        scale = float(scale) * 2.5
        os.environ['GDK_SCALE'] = str(scale)
    
    import gi
    
    
    gi.require_version("Gtk", "3.0")
    from gi.repository import Gtk, GLib
    
    
    class EntryDialog(Gtk.Dialog):
        def __init__(self, labeltext=None):
            super().__init__(title="Password", modal=True, focus_on_map=True)
            content = self.get_content_area()
            self.timeout_id = None
            vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,
                           spacing=15, margin=30)
            content.add(vbox)
    
            if labeltext is None:
                labeltext = "Please enter a password:"
            label = Gtk.Label(label=labeltext)
            vbox.pack_start(label, True, True, 0)
    
            hbox = Gtk.Box(spacing=1)
            self.entry = Gtk.Entry()
            self.entry.set_text("")
            self.entry.set_max_length(256)
            self.entry.set_invisible_char('•')
            self.entry.set_visibility(False)
            hbox.pack_start(self.entry, True, True, 0)
            self.show = Gtk.ToggleButton(label="show")
            self.show.set_active(False)
            hbox.pack_start(self.show, True, True, 0)
            vbox.pack_start(hbox, True, True, 0)
    
            #self.entry.connect("activate", lambda x: print("Enter"))
            self.show.connect("toggled", self.on_show_toggled)
            self.add_buttons(
                Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                Gtk.STOCK_OK, Gtk.ResponseType.OK
            )
            self.set_default_response(Gtk.ResponseType.OK)
            self.entry.connect("activate", self.on_entry_enter)
    
        def on_show_toggled(self, button):
            active = button.get_active()
            self.entry.set_visibility(active)
            button.set_label("hide" if active else "show")
    
        def on_entry_enter(self, button):
            self.activate_default()
    
        def get_password(self):
            return self.entry.get_text()
    
    
    def run_dialog(argv):
        if len(argv) == 1:
            win = EntryDialog()
        elif len(argv) == 2:
            win = EntryDialog(argv[1])
        else:
            print(f"Usage: {argv[0]} [<prompt text>]", file=sys.stderr)
            sys.exit(2)
        win.show_all()
        result = win.run()
        if result == Gtk.ResponseType.OK:
            print(win.get_password())
        else:
            sys.exit(1)
    
    
    if __name__ == '__main__':
        run_dialog(sys.argv)
    

    Note that I had to make sure the environment variable was set before I imported the gi package.