Alright, so to start I'm using Python 3.7 with tkinter.
(好的,首先,我将Python 3.7与tkinter一起使用。)
I have a canvas and I can drag a label around with that using the mouse events.
(我有一个画布,可以使用鼠标事件在其中拖动标签。)
My next step is to be able to drag around something on which I can put more widgets. (我的下一步是能够拖动一些我可以放置更多小部件的东西。)
So imagine a box which has a text box and an image on it, perhaps a combobox too. (因此,想象一个盒子上有一个文本框和一个图像,也许还有一个组合框。)
That box can then be dragged around. (然后可以拖动该框。)
I figured perhaps what I needed was a frame widget on my canvas which I could then set up in the same way as I had done with the label.
(我想也许我需要的是画布上的框架小部件,然后可以按照与标签相同的方式进行设置。)
But this is where it seems to fall apart - clearly I'm doing something wrong. (但这似乎是崩溃的地方-显然我做错了。)
Here's the code I've been playing around with, to no avail:
(这是我一直在玩的代码,无济于事:)
root = tk.Tk()
root.geometry("800x600")
def ClickedCallback(event):
print(f'Clicked: coords: {event.x}, {event.y}')
def ReleaseCallback(event):
print(f'Released: coords: {event.x}, {event.y}')
def MotionCallback(event):
print(f'Motion: coords: {event.x}, {event.y}')
canvas.coords(frame_id, event.x, event.y)
canvas = tk.Canvas(root, width=1000, height=600, bg='blue')
canvas.bind("<B1-Motion>", MotionCallback)
canvas.pack()
frame = tk.Frame(canvas, bg='green')
frame.pack()
l2 = tk.Label(frame, bg='red')
l2.bind("<Button-1>", ClickedCallback)
l2.pack()
l2['text'] = "Test"
frame_id = canvas.create_window((300,300), window=frame)
label_id = canvas.create_window((100, 100), window=l2)
root.mainloop()
My thinking here is that I attach the frame to the canvas and then the label to the frame so that, f I move the frame, the label within it will be moved too.
(我在这里的想法是,将框架附加到画布上,然后将标签附加到框架上,以便在移动框架时,框架内的标签也将被移动。)
The above won't work though, and it tells me the following:
(上面的方法虽然行不通,但它告诉我以下内容:)
File "C:\Users\JohnSmith\AppData\Local\Programs\Python\Python37-32\lib\tkinter__init__.py", line 2480, in _create *(args + self._options(cnf, kw)))) _tkinter.TclError: can't use .!canvas.!frame.!label in a window item of this canvas
(_create *(args + self._options(cnf,kw))))??_tkinter中的文件“ C:\ Users \ JohnSmith \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ tkinter__init __。py”,第2480行。 TclError:无法在此画布的窗口项中使用。!canvas。!frame。!label)
I may be going about this in entirely the wrong way.
(我可能会以完全错误的方式进行此操作。)
I had looked around for something more, but if there's something I'm doing entirely wrong or an established way I should be doing this, I would appreciate it if you could point me in the right direction. (我已经寻找了更多东西,但是如果我做某件事是完全错误的或者我应该这样做的既定方式,那么如果您能指出正确的方向,我将不胜感激。)
ask by TheFaithfulLearner translate from so