将一个tkinter画布项目放在位于同一画布上的其他tkinter小部件的顶部?

huangapple go评论58阅读模式
英文:

How to put a tkinter canvas item in top of other tkinter widgets placed in the same canvas?

问题

我正在尝试将一个放置在画布内的 tkinter 小部件置于画布项的后面。我尝试了 tag_raise 方法,但它不起作用。此外,我希望它们在同一个画布上。

是否有其他可能的方法?

英文:

I am trying to put a tkinter widget (placed inside canvas) behind a canvas item. I tried tag_raise method but it is not working. Moreover, I want them in the same canvas.

Is there any other possible way?

import tkinter

root = tkinter.Tk()
canvas = tkinter.Canvas(root)
canvas.pack()

canvas_widget = tkinter.Button(canvas, text="Hide this")
canvas_widget.place(x=25,y=30)

canvas_item = canvas.create_oval(10,10, 100,100, fill="blue")
canvas.tag_raise(canvas_item)

root.mainloop()

答案1

得分: 1

要将Windows小部件放入画布中,您可以使用画布的create_window方法,以便画布管理预先创建的Tk小部件。然而,Tk Canvas手册 指出:

注意:由于窗口管理方式的限制,不可能在窗口项目的顶部绘制其他图形项(如线条和图像)。

要在按钮上放置一个圆圈,您可以创建一个图像并在按钮上使用它。只需确保在您的应用程序类实例中保留对该图像的引用。

英文:

To put a windows widget into a canvas you use the create_window method of the canvas to have the canvas manage a pre-created Tk widget. However, the Tk Canvas man page points out:

> Note: due to restrictions in the ways that windows are managed, it is not possible to draw other graphical items (such as lines and images) on top of window items

To put a circle on a button, you can create an image and use that on the button. Just ensure you retain a reference to the image in your application class instance.

答案2

得分: 1

以下是翻译好的部分:

可以将Canvas椭圆对象放在按钮上,并使它们一起操作。

这个简单的演示使用tags来连接画布窗口对象。

代码注释进行了解释。

单击鼠标按钮1附近的按钮,以选择和放置按钮/椭圆对象。

英文:

It is possible to place a Canvas Oval object over a Button and have them act as one.

This simple demo uses tags to join canvas window objects together.

Code comments explain how.

Click mouse button 1 NEAR Button to pick and place Button/Oval objects
"""

import tkinter as tk

M = tk.Tk()
M.columnconfigure(0, weight = 1)
M.rowconfigure(0, weight = 1)
# Make Main Canvas and Button
C = tk.Canvas(M, width = 400, height = 400)
C.grid(sticky = tk.NSEW)
B = tk.Button(C, text = "       Press Me")
B.grid(sticky = tk.NSEW)
# Insert B into window W1
W1 = C.create_window(10, 10, window = B, anchor = tk.NW, tags = "A")

# Make Local Canvas c
c = tk.Canvas(C, width = 16, height = 16)
# Now make Oval
O = c.create_oval(2, 2, 16, 16, fill = "blue")
# Insert O into window W2
W2 = C.create_window(13, 13, window = c, anchor = tk.NW, tags = "A")

# Use tags to move both W1 and W2 as single object
flag = 0

# Demonstration of dual movement using tags
def mover(e):
    global flag, x, y, xx, yy
    x, y = e.x, e.y
    if e.num == 1 and int(e.type) == 4:
        flag = 1 - flag
    if flag:
        C.move("A", x-xx, y-yy)
    xx, yy = x, y

# bind 
C.event_add("<<CC>>", "<Motion>", "<Button-1>")
C.bind("<<CC>>", mover)

M.mainloop()

huangapple
  • 本文由 发表于 2023年5月10日 17:34:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定