Saya memiliki kode ini untuk membuat dialog kata sandi saya sendiri di Gtk. Saya perlu meningkatkannya 2-3 kali untuk tampilan yang akan digunakan. Bagaimana cara melakukannya?

#!/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)
0
Omnifarious 19 November 2020, 23:09

1 menjawab

Jawaban Terbaik

Menggunakan variabel lingkungan GDK_SCALE disarankan di IRC. Ini berfungsi untuk kasus penggunaan saya, tetapi tampaknya solusi yang buruk untuk penggunaan umum. Dalam kasus saya, inilah yang harus saya ubah program saya menjadi ini:

#!/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)

Perhatikan bahwa saya harus memastikan variabel lingkungan disetel sebelum saya mengimpor paket gi.

0
Omnifarious 20 November 2020, 16:35