在GTK +六角按钮 [英] hexagon buttons in GTK+

查看:131
本文介绍了在GTK +六角按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建 GTK + 按钮,有一个六边形的形状。
我怎样才能做到这一点,而无需使用 CSS

I am trying to create a button in GTK+ that has the shape of a Hexagon. How can I do it without using CSS?

和更一般的,我怎么能在我想要的任何形状创建我的按钮?

And more general, how can I create my button in any shape I want?

是否有可能在格莱德做这种事情(用户界面edior为GTK +)?

Is it possible to do such things in Glade(User interface edior for GTK+)?

推荐答案

当我把我的评论我在虚张声势,因为我从来没有圆形按钮..我只是用我原来的想法做了一个六角形按钮的一个例子。我吃了一惊,这是比我想象的更简单!

When I put my comment I was bluffing because I never did the circular button.. I just did an example of an Hexagonal button using my original idea.. I got surprised that it was simpler than I thought!

(顺便说一句,不,这是不可能做到这一点的空地!)

( and by the way, no, it is not possible to do it in glade! )

# 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()


我觉得用这个来解决问题的唯一不方便的是,用户的主题是不尊重。如果这是一个问题,而不是使用图像您可以通过使用一个绘图区,并获得主题颜色,我建议这里绘制按钮!

I think that the only inconvenient of using this to solve your problem is that the theme of the user is not respected. If that is a problem, instead of using an image you could draw your button by using a DrawingArea and by getting the theme colors as I suggested here!

我希望这是有益的:)

I hope it be useful :)

这篇关于在GTK +六角按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆