简体   繁体   English

如何使 GTK Rust 中的按钮看起来更像一个链接?

[英]How can I make a button in GTK Rust look more like a link?

So I have tried to make a button in a project I am working on in GTK Rust look more like a link using the following:因此,我尝试在 GTK Rust 中正在处理的项目中制作一个按钮,看起来更像是使用以下链接的链接:

.MainScreenCpt__button_superman_button {
  color: #FFFFFF;
  font-size: 16px;
  text-decoration: underline;
  cursor: pointer;
}

The corresponding main screen component file with the button with the text of superman looks like this:带有超人文本按钮的相应主屏幕组件文件如下所示:

let superman_button = button::create_text_button(self, "superman_button", "Superman");
superman_button.set_halign(gtk::Align::Start);
superman_box.add(&superman_button);

The text of underline and cursor pointer in the css selector causes my application to panic and not compile. css 选择器中的下划线文本和 cursor 指针导致我的应用程序崩溃并且无法编译。 Is there a way to turn that button to look more like a link?有没有办法让那个按钮看起来更像一个链接?

The error I get when it panics is:当它恐慌时我得到的错误是:

(process:1073867): GLib-GIO=CRITICAL **: 19:18:34.262: g_application_set_application_id: assertion 'application_id == NULL || g_application_id_is_valid (application_id)' failed thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { domain: gtk-css-provider-error-quark, code: 3, message: "<data>:8:10'cursor' is not a valid property name" }',
src/main.rs:35:62 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

So part of the problem was that I was calling the widget name of superman_button incorrectly, but even after I corrected that, if I try a所以部分问题是我错误地调用了superman_button的小部件名称,但即使在我更正之后,如果我尝试

.MainScreenCpt__button_superman_button {
  background-color: blue;
}

I get no response, and yet if I do:我没有得到回应,但如果我这样做:

.MainScreenCpt__button_superman_button label {
  color: #FFFFFF;
  font-size: 16px;
  text-decoration: underline;
}

That works, the label gets styled with no issues, but it still looks like a button.这行得通,label 的样式没有问题,但它看起来仍然像一个按钮。

It looks like the issue with your CSS is that the cursor and text-decoration properties are not valid for buttons in GTK. The background-color property should work, but you may need to make sure that the widget name is correctly referenced in your CSS.看起来你的 CSS 的问题是 cursor 和文本装饰属性对 GTK 中的按钮无效。背景颜色属性应该有效,但你可能需要确保在你的 CSS 中正确引用了小部件名称.

Regarding the styling of the button to look more like a link, you can try styling the label inside the button instead of the button itself.关于按钮的样式看起来更像一个链接,您可以尝试在按钮内设置 label 而不是按钮本身。 That's why.MainScreenCpt__button_superman_button label {...} works.这就是为什么.MainScreenCpt__button_superman_button label {...} 有效。 You can also try using an event box as a container for the button and apply the styling on the event box.您还可以尝试使用事件框作为按钮的容器,并在事件框上应用样式。

If you want to make the button look more like a link, you can try the following:如果你想让按钮看起来更像一个链接,你可以尝试以下方法:

  1. Style the label inside the button:在按钮内设置 label 的样式:

 .MainScreenCpt__button_superman_button label { color: #0000FF; /* blue */ font-size: 16px; text-decoration: underline; }

This will change the color and text decoration of the text inside the button to resemble a link.这将更改按钮内文本的颜色和文本装饰,使其类似于链接。

  1. Use an event box as a container for the button:使用事件框作为按钮的容器:

 let event_box = gtk::EventBox::new(); event_box.add(&superman_button); superman_box.add(&event_box);

And then in your CSS you can apply the following:然后在您的 CSS 中,您可以应用以下内容:

.MainScreenCpt__button_superman_button {cursor: pointer;
 background-color: blue;}

This will change the cursor to pointer and background color to blue, which will make it look more like a link.这会将 cursor 更改为指针,将背景颜色更改为蓝色,这将使它看起来更像一个链接。

Also Make sure you import the gtk::EventBox in your main.rs还要确保在 main.rs 中导入 gtk::EventBox

Reading your question, it seem to me that you are looking for some solution to make some text clickable (link in HTML).阅读您的问题,在我看来,您正在寻找一些解决方案来使某些文本可点击(HTML 中的链接)。 If that's correct then read further.如果那是正确的,那么请进一步阅读。

In below example I used gtk4, but document just that it should work with gtk3 as well.(With some changes in creating application/window object)在下面的示例中,我使用了 gtk4,但仅记录它也应该与 gtk3 一起使用。(在创建应用程序/窗口对象时进行了一些更改)

use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Button, Inhibit, Label, ListBox};

const APP_ID: &str = "org.gtk_rs.HelloWorld2";

fn main() {
    let app = Application::builder().application_id(APP_ID).build();
    app.connect_activate(build_ui);
    app.run();
}
fn build_button_with_label() -> Label {
    let label = Label::new(None);
    label.set_markup("Go to the <a href=\"https://www.gtk.org\" title=\"&lt;i&gt;Our&lt;/i&gt; website\">GTK website</a> for more...");
    label.connect_activate_link(|_label, link_text| {
        println!("Link target is {:?} getting clicked", link_text);
        // Inhibit(false) // return false to call os default behaviour
        Inhibit(true) // return true to not to call os default behaviour
    });
    label
}
fn build_ui(app: &Application) {
    let button = Button::builder()
        .label("Press me!")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    button.connect_clicked(|button| {
        button.set_label("Hello World!");
    });
    let label = build_button_with_label();
    let list_box = ListBox::new();
    list_box.append(&button);
    list_box.append(&label);

    let window = ApplicationWindow::builder()
        .application(app)
        .title("My GTK App")
        .child(&list_box)
        .build();
    window.present();
}

In GTK (using the Rust bindings), you can make a button look more like a link by customizing its appearance.在 GTK(使用 Rust 绑定)中,您可以通过自定义外观使按钮看起来更像一个链接。 Here's an example of how you can do this:以下是如何执行此操作的示例:

    use gtk::prelude::*;

// ...

let button = gtk::Button::new_with_label("Click me");
button.get_style_context().add_class("link-style");

In this example, we add a class named link-style to the button's style context. You can then define the appearance of this class in your CSS file:

    .link-style {
  color: #0000ff;
  text-decoration: underline;
  cursor: pointer;
}

This will make the button text blue, underlined, and have a pointer cursor, which is similar to the appearance of a link. You can adjust the CSS properties as needed to better match the desired appearance.

In order to apply the CSS, you can use the gtk::CssProvider and gtk::StyleContext APIs:



 let css = include_str!("your_css_file.css");
    let provider = gtk::CssProvider::new();
    provider.load_from_data(css.as_bytes()).expect("Failed to load CSS");

let screen = gdk::Screen::get_default().expect("Error initializing screen.");
gtk::StyleContext::add_provider_for_screen(&screen, &provider, gtk::STYLE_PROVIDER_PRIORITY_APPLICATION);

I ended up doing the following which works:我最终做了以下工作:

.SomeCpt__button_superman_button {
  // styling here 
}

.SomeCpt__button_superman_button:hover {
   color: #409EFF;
   background-color: rgb(236, 245, 255);
}

.SomeCpt__button_superman_button:hover label {
  text-decoration-line: underline; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM