根据鼠标单击坐标突出显示画布弧

时间:2019-07-01 07:44:18

标签: python python-3.x canvas tkinter

我正在使用圆形导航栏,单击菜单选项之一会使它更改颜色(如果单击另一个,则会恢复为原始颜色)。

使用realloc方法,我为每个按钮(表示为文本)创建了边框,并使用字典,试图将按钮的文本存储为键,并将按钮和arc标记存储为代表导航栏菜单选项的值。

但是,即使每个文本项都以正确的弧线存储,当单击一个选项时,也会突出显示错误的弧线。这是我所能完成的最接近的工作:

create_arc

忽略文本的旋转不良,上面的代码段几乎起作用,除了最后一个按钮(“退出”)外-它不会改变颜色。每个文本项如何与圆弧关联似乎存在问题。

弧线沿逆时针方向延伸,因此我尝试以类似的逆时针方式创建按钮,以将每个弧线与文本项相关联,但是我不确定最终文本项在哪里出问题。

我还尝试过反转import tkinter as tk import math class CircularNavbar(tk.Frame): def __init__(self, parent, font='arial', fontsize=20, textcolour='red', highlightcolour='blue', selectcolour='light blue', btncolour='white', outlinecolour='black', navsize=500, btnwidth=50, options=[], *args, **kwargs): tk.Frame.__init__(self, parent, *args, **kwargs) self.font, self.fontsize = font, fontsize self.textcolour, self.highlightcolour = textcolour, highlightcolour self.selectcolour = selectcolour self.btncolour, self.outlinecolour = btncolour, outlinecolour self.navsize, self.btnwidth, self.max_btns = navsize, btnwidth, len(options) self._children, self.options, self.btns = {}, options, [] self.canvas = tk.Canvas(self, width=navsize, height=navsize) self.canvas.grid() d = 360 // self.max_btns for count in range(-d//2, 360-d//2, d): btn = self.canvas.create_arc(navsize*0.1, navsize*0.1, navsize*0.9, navsize*0.9, outline=btncolour, start=count, extent=d-1, style=tk.ARC, width=btnwidth*0.9, tag=count, activefill=selectcolour, disabledfill=btncolour) self.btns.append(btn) for count in range(self.max_btns): btn = self.canvas.create_text(navsize//2, navsize*0.1, font=(font, fontsize), fill=textcolour, activefill=highlightcolour, text=self.options[count], tag=self.options[count]) self._position(btn, count) self._children[self.options[count]] = [btn, self.btns[count-7]] self.canvas.tag_bind(btn, '<Button-1>', lambda event, text=self.options[count]: self._setindicator(event, text)) def _position(self, btn, count): angle = count*(360/self.max_btns) x = -math.sin(math.radians(angle)) * self.navsize*0.4 + self.navsize//2 y = -math.cos(math.radians(angle)) * self.navsize*0.4 + self.navsize//2 self.canvas.coords(btn, x, y) self.canvas.itemconfig(btn, angle=angle) self.canvas.tag_raise(btn) def _setindicator(self, event=None, text=None): for option, btns in self._children.items(): if text == option: self.canvas.itemconfig(btns[1], state='disabled', outline=self.selectcolour) self.canvas.itemconfig(btns[0], state='disabled', fill=self.outlinecolour) else: self.canvas.itemconfig(btns[1], state='normal', outline=self.btncolour) self.canvas.itemconfig(btns[0], state='normal', fill=self.textcolour) win.update() options = ['Play', 'Settings', 'Exit', 'Buy', 'Boards', 'Help', 'Shop', 'Quit'] win = tk.Tk() Nav = CircularNavbar(win, font='console', fontsize=22, textcolour='#369efb', highlightcolour='blue', selectcolour='#68b6fc', btncolour='#0478e1', outlinecolour='#035096', navsize=700, btnwidth=70, options=options) Nav.grid() win.mainloop() 列表,反转第一个for循环的增量(并尝试self.btns-count等-在将圆弧添加到字典时:{ {1}}),并使用逆时针或顺时针按钮,但无济于事...

0 个答案:

没有答案