gpt4 book ai didi

c - GTK+ 中的六边形按钮

转载 作者:太空狗 更新时间:2023-10-29 15:38:32 25 4
gpt4 key购买 nike

我正在尝试在 GTK+ 中创建一个具有六边形形状的按钮。不使用 CSS 怎么办?

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

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

enter image description here enter image description here

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

希望有用:)

关于c - GTK+ 中的六边形按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23957525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com