| 1 |
import gi |
| 2 |
|
| 3 |
gi.require_version("Gtk", "4.0") |
| 4 |
from gi.repository import Gtk, GLib |
| 5 |
|
| 6 |
|
| 7 |
class EntryWindow(Gtk.ApplicationWindow): |
| 8 |
def __init__(self, **kargs): |
| 9 |
super().__init__(**kargs, title="Entry Demo") |
| 10 |
|
| 11 |
self.set_size_request(200, 100) |
| 12 |
|
| 13 |
self.timeout_id = None |
| 14 |
|
| 15 |
header = Gtk.HeaderBar() |
| 16 |
self.set_titlebar(header) |
| 17 |
|
| 18 |
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6) |
| 19 |
vbox.props.margin_start = 24 |
| 20 |
vbox.props.margin_end = 24 |
| 21 |
vbox.props.margin_top = 24 |
| 22 |
vbox.props.margin_bottom = 24 |
| 23 |
self.set_child(vbox) |
| 24 |
|
| 25 |
# Gtk.SearchEntry |
| 26 |
search = Gtk.SearchEntry() |
| 27 |
search.props.placeholder_text = "Search Entry" |
| 28 |
search.set_key_capture_widget(self) |
| 29 |
header.set_title_widget(search) |
| 30 |
self.set_focus(search) |
| 31 |
|
| 32 |
# Gtk.Entry |
| 33 |
self.entry = Gtk.Entry() |
| 34 |
self.entry.set_text("Hello World") |
| 35 |
vbox.append(self.entry) |
| 36 |
|
| 37 |
hbox = Gtk.Box(spacing=6) |
| 38 |
vbox.append(hbox) |
| 39 |
|
| 40 |
self.check_editable = Gtk.CheckButton(label="Editable") |
| 41 |
self.check_editable.connect("toggled", self.on_editable_toggled) |
| 42 |
self.check_editable.props.active = True |
| 43 |
hbox.append(self.check_editable) |
| 44 |
|
| 45 |
self.check_visible = Gtk.CheckButton(label="Visible") |
| 46 |
self.check_visible.connect("toggled", self.on_visible_toggled) |
| 47 |
self.check_visible.props.active = True |
| 48 |
hbox.append(self.check_visible) |
| 49 |
|
| 50 |
self.pulse = Gtk.CheckButton(label="Pulse") |
| 51 |
self.pulse.connect("toggled", self.on_pulse_toggled) |
| 52 |
hbox.append(self.pulse) |
| 53 |
|
| 54 |
self.icon = Gtk.CheckButton(label="Icon") |
| 55 |
self.icon.connect("toggled", self.on_icon_toggled) |
| 56 |
hbox.append(self.icon) |
| 57 |
|
| 58 |
# Gtk.PasswordEntry |
| 59 |
pass_entry = Gtk.PasswordEntry() |
| 60 |
pass_entry.props.placeholder_text = "Password Entry" |
| 61 |
pass_entry.props.show_peek_icon = True |
| 62 |
pass_entry.props.margin_top = 24 |
| 63 |
vbox.append(pass_entry) |
| 64 |
|
| 65 |
def on_editable_toggled(self, button): |
| 66 |
value = button.get_active() |
| 67 |
self.entry.set_editable(value) |
| 68 |
|
| 69 |
def on_visible_toggled(self, button): |
| 70 |
self.entry.props.visibility = button.props.active |
| 71 |
|
| 72 |
def on_pulse_toggled(self, button): |
| 73 |
if button.get_active(): |
| 74 |
self.entry.props.progress_pulse_step = 0.2 |
| 75 |
# Call self.do_pulse every 100 ms |
| 76 |
self.timeout_id = GLib.timeout_add(100, self.do_pulse) |
| 77 |
else: |
| 78 |
# Don't call self.do_pulse anymore |
| 79 |
GLib.source_remove(self.timeout_id) |
| 80 |
self.timeout_id = None |
| 81 |
self.entry.props.progress_pulse_step = 0 |
| 82 |
|
| 83 |
def do_pulse(self): |
| 84 |
self.entry.progress_pulse() |
| 85 |
return True |
| 86 |
|
| 87 |
def on_icon_toggled(self, button): |
| 88 |
icon_name = "system-search-symbolic" if button.props.active else None |
| 89 |
self.entry.set_icon_from_icon_name(Gtk.EntryIconPosition.PRIMARY, icon_name) |
| 90 |
|
| 91 |
|
| 92 |
def on_activate(app): |
| 93 |
win = EntryWindow(application=app) |
| 94 |
win.present() |
| 95 |
|
| 96 |
|
| 97 |
app = Gtk.Application(application_id="com.example.App") |
| 98 |
app.connect("activate", on_activate) |
| 99 |
|
| 100 |
app.run(None) |