【问题标题】:How to get the id of canvas item?如何获取画布项目的ID?
【发布时间】:2019-11-03 12:35:17
【问题描述】:

如果我将画布项目存储在一个变量中,我希望该变量存储为一个 tkinter.rectangle 对象,我以后可以使用它。

rec = can.create_rectangle(l, fill="blue")

而是存储为整数>

from tkinter import Tk, Canvas, Button

def press(canv, rect):
    print("pressed")
    canv.move(rect, 10)

l = [50,100,100,200]

root = Tk()
can = Canvas(root)
can.pack()
rec = can.create_rectangle(l, fill="blue")
print("rec",rec)    #1
print("type(rec) ", type(rec)) #<class 'int'>
b = Button(root, text="NOTHING", command=lambda:press(can, rec))
b.pack()
print("type(b) = ",type(b)) #<class 'tkinter.Button'>
print("b = ",b) #TCL id like .41549040
root.mainloop()

运行此代码时会返回错误:

_tkinter.TclError: wrong # args: should be ".21823184 move tagOrId xAmount yAmount"

为什么它是整数类型以及如何获取画布项的 id 以便以后移动它?

【问题讨论】:

  • _tkinter.TclError: wrong # args::你误会了,这与rec无关。阅读Tkinter.Canvas.move-method的参数
  • 那是什么意思它不是关于rec?
  • _tkinter.TclError: 不是关于 rec 它是关于缺少参数 x_movey_move
  • 好的,现在我明白了,谢谢

标签: python tkinter canvas


【解决方案1】:

如果我将画布项目存储在我希望存储为 tkinter.rectangle 对象的变量中

这是一个错误的期望。记录的行为是它返回一个整数 id。

为什么它是整数类型以及如何获取画布项的 id 以便以后移动它?

除了create_rectangle 方法返回的整数之外,没有其他id。您可以使用此 id 来移动项目。

运行此代码时会返回错误:_tkinter.TclError: wrong # args: should be ".21823184 move tagOrId xAmount yAmount"

该错误消息准确地告诉您出了什么问题:wrong # args。您必须提供 id、xAmount 和 yAmount。您只提供 id 和 xAmount。

【讨论】:

  • 你知道为什么我会因为提出更多问题而被禁止吗?或者为什么这个问题不好,以便我知道改进我的进一步问题?
  • @Module_art:我不知道为什么。这个问题对我来说似乎没问题。
  • 感谢您的宝贵时间
【解决方案2】:

解决方案很简单:您不给 Y 值赋予功能。 函数要:

canv.move(item, x, y)

如果你只需要在x轴上移动它,你必须写下面的字符串:

canv.move(rect, 10, 0)

【讨论】:

    猜你喜欢
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    相关资源
    最近更新 更多