【问题标题】:Is there a way to have a draggable ruler in tkinter?有没有办法在 tkinter 中有一个可拖动的标尺?
【发布时间】:2019-10-14 19:03:35
【问题描述】:

有可能吗?

只需将它放在窗口上的任何位置,然后将其拖动到我想要的任何位置。

这是我希望从 HTML 中实现的可拖动项目的示例(我知道,它与 html 无关):How TO - Create a Draggable HTML Element

这是我所说的尺子的一个例子。像这样的尺子:

仅用于显示目的,不计算任何东西..

在这种情况下,我将使用网格管理器。

我很乐意看到任何示例!

【问题讨论】:

  • 是的,我想是的。见问题Drag and Drop widgets tkinter
  • 正如所写,这个问题太宽泛了。
  • 嗨,我很高兴看到任何例子..
  • 使用place(),您可以随意放置,拖动可能是个小问题,但问题可能是使用线条和数字创建规则,并在规则更改位置时重新计算规则的值。您必须从头开始创建规则。所以这是一个广泛的问题。
  • 仅在解决了@furas 的小问题时才用于显示?

标签: python tkinter


【解决方案1】:

标准模块tkinterttk 没有标尺,我不知道tkinter 有任何带有标尺的外部模块。


使用Canvas 我可以创建用数字绘制线条的小部件。

但它仍然是原始小部件,不会调整大小,不会滚动线条和数字,不会重新缩放,也不会显示鼠标位置。

编辑:现在规则使用红线显示鼠标位置。但是如果没有Canvas,那么他们必须知道偏移量——他们离窗口的左上角有多远。

import tkinter as tk

class VRuler(tk.Canvas):
    '''Vertical Ruler'''

    def __init__(self, master, width, height, offset=0):
        super().__init__(master, width=width, height=height)
        self.offset = offset

        step = 10

        # start at `step` to skip line for `0`
        for y in range(step, height, step):
            if y % 50 == 0:
                # draw longer line with text
                self.create_line(0, y, 13, y, width=2)
                self.create_text(20, y, text=str(y), angle=90)
            else:
                self.create_line(2, y, 7, y)

        self.position = self.create_line(0, 0, 50, 0, fill='red', width=2)

    def set_mouse_position(self, y):
        y -= self.offset
        self.coords(self.position, 0, y, 50, y) 

class HRuler(tk.Canvas):
    '''Horizontal Ruler'''

    def __init__(self, master, width, height, offset=0):
        super().__init__(master, width=width, height=height)
        self.offset = offset

        step = 10

        # start at `step` to skip line for `0`
        for x in range(step, width, step):
            if x % 50 == 0:
                # draw longer line with text
                self.create_line(x, 0, x, 13, width=2)
                self.create_text(x, 20, text=str(x))
            else:
                self.create_line((x, 2), (x, 7))

        self.position = self.create_line(0, 0, 0, 50, fill='red', width=2)

    def set_mouse_position(self, x):
        x -= self.offset
        self.coords(self.position, x, 0, x, 50) 

def motion(event):
    x, y = event.x, event.y
    hr.set_mouse_position(x)
    vr.set_mouse_position(y)

def click(event):
    print(event.x, event.y)

root = tk.Tk()
root['bg'] = 'black'

vr = VRuler(root, 25, 250)#, offset=25)
vr.place(x=0, y=28)

hr = HRuler(root, 250, 25)#, offset=25)
hr.place(x=28, y=0)

c = tk.Canvas(root, width=250, height=250)
c.place(x=28, y=28)

#root.bind('<Motion>', motion) # it needs offset=28 if there is no Canvas
#root.bind('<Button-1>', click)
c.bind('<Motion>', motion)
c.bind('<Button-1>', click)

root.mainloop()

【讨论】:

    猜你喜欢
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 2017-01-04
    • 2019-09-09
    相关资源
    最近更新 更多