gtk(mm) 3按钮背景颜色改变
我正在尝试将按钮的背景颜色更改为红色,但似乎不起作用。 我粘贴示例代码。 如果有人可以告诉我如何修复我的代码,请帮忙。
#include <gtkmm.h>
// g++ simple.cc -o simple `pkg-config gtkmm-3.0 --cflags --libs`
int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
Gtk::Window window;
Gtk::Button button("TEST");
button.override_background_color(Gdk::RGBA("red"));
window.add(button);
window.show_all();
Gtk::Main::run(window);
return EXIT_SUCCESS;
}
更新: 好的,这就是我解决的方法:
mr_screen = Gdk::Screen::get_default();
mr_style_context = mp_window->get_style_context();
mr_css_provider = Gtk::CssProvider::create();
mr_css_provider->load_from_path(Glib::build_filename(m_glade_dir_path, "filename.css"));
mr_style_context->add_provider_for_screen(mr_screen, mr_css_provider, GTK_STYLE_PROVIDER_PRIORITY_USER);
filename.css 的内容是:
column-header .button {
background-image: -gtk-gradient (linear,
left top,
left bottom,
from (#51cccc),
color-stop (0.5, darker (#51cccc)),
to (#51cccc));
}
I'm trying to change the background color of a button to red but it doesn't seem to work.
I paste the example code.
If anybody can tell me how to fix my code please help.
#include <gtkmm.h>
// g++ simple.cc -o simple `pkg-config gtkmm-3.0 --cflags --libs`
int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
Gtk::Window window;
Gtk::Button button("TEST");
button.override_background_color(Gdk::RGBA("red"));
window.add(button);
window.show_all();
Gtk::Main::run(window);
return EXIT_SUCCESS;
}
UPDATE:
ok here's how I solved:
mr_screen = Gdk::Screen::get_default();
mr_style_context = mp_window->get_style_context();
mr_css_provider = Gtk::CssProvider::create();
mr_css_provider->load_from_path(Glib::build_filename(m_glade_dir_path, "filename.css"));
mr_style_context->add_provider_for_screen(mr_screen, mr_css_provider, GTK_STYLE_PROVIDER_PRIORITY_USER);
and the content of filename.css is:
column-header .button {
background-image: -gtk-gradient (linear,
left top,
left bottom,
from (#51cccc),
color-stop (0.5, darker (#51cccc)),
to (#51cccc));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实上,我经历了惨痛的教训才明白,CssProvider 是处理此类事情的正确方法。你不需要使用glade,最好是在你的代码中包含css,就像这样
你必须处理background-image,因为background-color与默认background-image重叠,因此不会改变。
我的智慧来自 Ruby-GNOME2
Just indeed, I learned it the hard way, that CssProvider is the right way to handle such things. You don't need to use glade, even good is to have css in your code, like so
You have to deal with background-image, because background-color is overlapped from the default background-image and thus not changed.
I have my wisdom from Ruby-GNOME2
我没有c++经验,但我使用python,在python中你必须这样做:
也许在c++中你必须这样做
i have no experience with c++, but i use python, in python you have to do this:
maybe in c++ you have to do this