pygtk entry.set_placeholder_text不起作用 - 缺少tex

发布于 2025-01-20 09:14:51 字数 524 浏览 3 评论 0原文

我正在附上一块代码,其中我尝试用预定义的文本在入口字段中显示文本。我为此使用entry_placeholder_text,但不幸的是文本未显示。 有有效的解决方案吗?

# -*- coding: utf-8 -*-
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

class MyWindow(Gtk.Window):
    def __init__(self):
        super(MyWindow, self).__init__()
        entry = Gtk.Entry()
        entry.set_placeholder_text("Any text")
        self.add(entry)

win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

I am attaching a piece of my code in which I try to display the text in the Entry field with predefined text. I use entry.set_placeholder_text for this, but unfortunately the text does not appear.
Is there an effective solution?

# -*- coding: utf-8 -*-
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

class MyWindow(Gtk.Window):
    def __init__(self):
        super(MyWindow, self).__init__()
        entry = Gtk.Entry()
        entry.set_placeholder_text("Any text")
        self.add(entry)

win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦忆晨望 2025-01-27 09:14:51

仅当条目为空且未聚焦时,占位符文本才可见。添加另一个可以获得焦点的小部件将使文本出现。

例子:

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

class MyWindow(Gtk.Window):
    def __init__(self):
        super(MyWindow, self).__init__()
        entry = Gtk.Entry()
        entry.set_placeholder_text("Any text")
        box = Gtk.HBox()
        self.add(box)
        button = Gtk.Button()
        box.pack_start(button, False, False, 0)
        box.pack_start(entry, True, True, 0)

win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

The placeholder text is only visible when the entry is empty and unfocused. Adding another widget that can get the focus will make the text appear.

Example:

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

class MyWindow(Gtk.Window):
    def __init__(self):
        super(MyWindow, self).__init__()
        entry = Gtk.Entry()
        entry.set_placeholder_text("Any text")
        box = Gtk.HBox()
        self.add(box)
        button = Gtk.Button()
        box.pack_start(button, False, False, 0)
        box.pack_start(entry, True, True, 0)

win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
我做我的改变 2025-01-27 09:14:51

我知道,我不知道为什么它不起作用。
我通常这样做:

 search_entry.props.placeholder_text = 'Placeholder text'

在小部件上调用 set_active 之前

I know, I have no idea why it doesn't work.
I usually do:

 search_entry.props.placeholder_text = 'Placeholder text'

before calling set_active on the widget

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文