[英]Gtk3+ How to change Gtk.Entry border to a color?
我有一个 Gtk.Entry 并且想在用户输入无效值时将他的边框更改为红色。 我正在使用 Gtk+3 和 Python3。 对此有任何意见吗? 我已经看到“Gtk.Entry.set_inner_border()”已被 gtk+ > 3.4 弃用。
编辑1:
似乎我的 Gtk 版本或我的操作系统根本不喜欢颜色! 我也有这两行代码,我的按钮没有任何颜色:
button1.get_style_context().add_class('suggested-action')
button2.get_style_context().add_class(Gtk.STYLE_CLASS_DESTRUCTIVE_ACTION)
我的 Gtk+3 版本是 3.18。 估计是这个原因? 您会建议我升级或使用小部件 set_color 函数吗?
我将进行调查,在最坏的情况下,我可能会使用我测试过的 Gtk.Widget.set_background_color 并且工作正常,即使它不会影响边框的颜色而是文本的背景。 现在我应该弄清楚如何自动选择文本呵呵。
无论如何,非常感谢你,何塞
编辑2:
以下行不显示颜色:
b_add.get_style_context().add_class('suggested-action')
b_remove.get_style_context().add_class(Gtk.STYLE_CLASS_DESTRUCTIVE_ACTION)
Gtk+ 3.x 使用 CSS 来设置小部件的样式和主题。 CSS 结构、样式类确实从 3.0 更改为 3.26。 这意味着了解您正在使用的版本很重要。
使用 Gtk+ 3.22,您可以使用 css:
entry {
border-color: Red;
}
使用 Gtk+ 3.18 使用:
.entry {
border-color: Red;
}
将此 css 代码复制到名为 test.css 的文件中,然后使用Python gt3 教程中的此改编示例:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GObject
class EntryWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Entry Demo")
self.set_size_request(200, 100)
self.timeout_id = None
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
self.add(vbox)
self.entry = Gtk.Entry()
self.entry.set_text("Hello World")
vbox.pack_start(self.entry, True, True, 0)
hbox = Gtk.Box(spacing=6)
vbox.pack_start(hbox, True, True, 0)
self.check_editable = Gtk.CheckButton("Editable")
self.check_editable.connect("toggled", self.on_editable_toggled)
self.check_editable.set_active(True)
hbox.pack_start(self.check_editable, True, True, 0)
self.check_visible = Gtk.CheckButton("Visible")
self.check_visible.connect("toggled", self.on_visible_toggled)
self.check_visible.set_active(True)
hbox.pack_start(self.check_visible, True, True, 0)
self.pulse = Gtk.CheckButton("Pulse")
self.pulse.connect("toggled", self.on_pulse_toggled)
self.pulse.set_active(False)
hbox.pack_start(self.pulse, True, True, 0)
self.icon = Gtk.CheckButton("Icon")
self.icon.connect("toggled", self.on_icon_toggled)
self.icon.set_active(False)
hbox.pack_start(self.icon, True, True, 0)
def on_editable_toggled(self, button):
value = button.get_active()
self.entry.set_editable(value)
def on_visible_toggled(self, button):
value = button.get_active()
self.entry.set_visibility(value)
def on_pulse_toggled(self, button):
if button.get_active():
self.entry.set_progress_pulse_step(0.2)
# Call self.do_pulse every 100 ms
self.timeout_id = GObject.timeout_add(100, self.do_pulse, None)
else:
# Don't call self.do_pulse anymore
GObject.source_remove(self.timeout_id)
self.timeout_id = None
self.entry.set_progress_pulse_step(0)
def do_pulse(self, user_data):
self.entry.progress_pulse()
return True
def on_icon_toggled(self, button):
if button.get_active():
icon_name = "system-search-symbolic"
else:
icon_name = None
self.entry.set_icon_from_icon_name(Gtk.EntryIconPosition.PRIMARY,
icon_name)
win = EntryWindow()
style_provider = Gtk.CssProvider()
style_provider.load_from_path("test.css")
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(),
style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
结果应该类似于:
编辑:
Fedora 23 (Gtk+ 3.18.9) 上的结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.