c – GTK中的六角形按钮

我正在尝试在GTK中创建一个具有Hexagon形状的按钮.

如何在不使用CSS的情况下完成?

更一般的是,如何以我想要的任何形状创建我的按钮?

是否有可能在Glade(用户界面edior for GTK)中做这些事情?

最佳答案 当我发表评论时,我在虚张声势,因为我从未做过圆形按钮……我只是用我原来的想法做了一个六角形按钮的例子.我感到惊讶的是它比我想象的更简单!

(顺便说一下,不,不可能在林间空地做!)

# sorry but the example is in Python! :/

from gi.repository import Gtk


def hexagon(coord_x, coord_y):

    # because of the symetry I take only the absolute value
    coord_x, coord_y= abs(coord_x), abs(coord_y)


    # I got the constants by clicling in the image and printing the coord_x and coord_y values

    if coord_x <= 13 and coord_y <= 25:   # define a rectangle
        return True
    else:
        # I cut the coord x to define a triangle
        coord_x=coord_x-13/2

        # line equation
        ymax=(-25/31)*coord_x+25

        if coord_y < ymax:
            return True
        else:
            return False


class GUI(Gtk.Window):

    def __init__(self):

        self.window_root=Gtk.Window()


        # Create an event box to handle the click's
        self.eventbox=Gtk.EventBox()
        self.eventbox.connect('button-press-event' , self.on_eventbox_pressed)
        self.eventbox.connect('button-release-event' , self.on_eventbox_released)

        # Load the images
        self.hexagon1=Gtk.Image.new_from_file('./3uSFN.png')
        self.hexagon2=Gtk.Image.new_from_file('./cWmUA.png')

        # init the event box
        self.eventbox.add(self.hexagon1)
        self.window_root.add(self.eventbox)     
        self.window_root.show_all()

        # a variable to store the state of the button
        self.state=False



    def on_eventbox_pressed(self, widget , event):

        if 'GDK_BUTTON_PRESS' in str(event.type): # If the user made a "single click"
            if event.button == 1: # if it is a left click

                # get the x,y of the mouse from the center of the image
                pos_x, pos_y=self.window_root.get_position()
                siz_x, siz_y=self.window_root.get_size()
                mouse_x,mouse_y=event.x-siz_x/2, siz_y/2-event.y

                if hexagon(mouse_x, mouse_y):
                    self.eventbox.remove(self.hexagon1)
                    self.eventbox.add(self.hexagon2)
                    self.eventbox.show_all()

                    self.state=True


    def on_eventbox_released(self, widget , event):
        if self.state:
            self.eventbox.remove(self.hexagon2)
            self.eventbox.add(self.hexagon1)
            self.state=False


main=GUI()
Gtk.main()

我认为使用它来解决问题的唯一不便之处是用户的主题不受尊重.如果这是一个问题,你可以使用DrawingArea绘制你的按钮,而不是使用图像,并按照我的建议获取主题颜色here!

我希望它有用:)

点赞