You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python报AttributeError: 'tuple'对象无'place'属性如何解决?

报错原因分析
  • 核心错误是 Button 实例化语法错误:你多写了一个等号,将所有参数用逗号包裹成了元组直接赋值给btn,此时btn是元组类型,不是tkinter的Button控件实例,自然没有place方法。同时tkinter控件的参数是关键字参数,不需要加引号,参数名和值之间用等号连接,command参数的分隔符也不能用冒号。
  • 次要错误是place方法的参数拼写错误,wigth为错误拼写,正确写法是width。
修复后可直接运行的代码
from tkinter import Tk, Button

# 点击按钮的回调函数,你可以自行修改逻辑
def click_button():
    click_count = int(btn["text"].split()[1])
    btn.config(text=f"Clicks {click_count + 1}")

# 初始化窗口
root = Tk()
root.geometry("300x200")

# 正确实例化Button控件
btn = Button(
    root,
    text="Clicks 0",
    background="#555",
    foreground="#ccc",
    padx=20,
    pady=8,
    font=16,
    command=click_button
)
# 修正width拼写错误
btn.place(relx=.5, rely=.5, anchor="c", height=30, width=130)

root.mainloop()
修复要点说明
  • 去掉Button后多余的等号,给Button传入父容器作为第一个参数,所有参数改为不加引号的关键字参数格式,command后的冒号修改为等号
  • 修正place方法里的宽度参数拼写错误
  • 补充了tkinter运行必须的窗口初始化、事件循环、点击回调函数逻辑

内容的提问来源于stack exchange,提问作者lol288988

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 15:57:05