python - AttributeError: 'Event' 对象没有属性 'path' plis ayuda
问题描述
quiero que el file_root salga en entry ya intente con insert AttributeError: 'Event' object has no attribute 'path'
import tkinter as tk
from tkinter import ttk
from tkinter import Tk, LEFT
from tkinter import filedialog
class Aplication:
def __init__(self):
self.root = tk.Tk()
self.add_components()
self.root.mainloop()
@staticmethod
def change_path(self):
file_root = filedialog.askopenfilename(initialdir = "/",title = "Select File",filetypes = (("Python3 file","*.py"),("Others","*.*")))
self.path.set(file_root)#el path obtenida por el file root aparesca en el entry
def add_components(self):
self.path = tk.Entry(self.root, state="readonly")
self.path.bind("<1>", self.change_path)
self.path.pack(side=LEFT)
if __name__ == "__main__":
app = Aplication()
解决方案
你混合了两种不同的方法——staticmethod
不能使用self
,因为它没有得到它。
你应该不写@staticmethod
还有其他问题。
Entry
没有.set(text)
但 `.insert('end', text)
当它是只读时我无法设置文本(至少在我的 Linux 上它不起作用),我必须切换到normal
,设置文本,切换到readonly
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
class Aplication:
def __init__(self):
self.root = tk.Tk()
self.add_components()
self.root.mainloop()
def change_path(self, event):
print('event:', event)
file_root = filedialog.askopenfilename(initialdir="/", title="Select File", filetypes=(("Python3 file","*.py"),("Others","*.*")))
#print(file_root)
self.path['state'] = 'normal'
self.path.insert('end', file_root)
self.path['state'] = 'readonly'
def add_components(self):
self.path = tk.Entry(self.root, state="readonly")
self.path.bind("<1>", self.change_path)
self.path.pack(side='left')
if __name__ == "__main__":
app = Aplication()
推荐阅读
- python - 根据python中内部嵌套元素是否相同创建新列表
- javascript - cypress xhr 请求重试 ontimeout
- java - 将字符串中的每个其他字母大写
- c++ - 文件 I/O 与 TCP 套接字,哪个在同一台机器内传输数据更快?
- python - Python Pexels API Per_page
- python - AI 模型无法正确存储最佳神经元集
- typescript - 打字稿声明接口,扩展 Map 以保留键顺序
- java - Google Cloud Dataflow 已成功完成工作日志“Missing next work index in”
- javascript - 将数组组合成多个级别
- c# - 退出门户时如何加力