首页 > 解决方案 > 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()

标签: pythontkinter

解决方案


你混合了两种不同的方法——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()

推荐阅读