Dmustache
@Dmustache
Python, Cpp, SQL

Как показать путь к выбранному пользователем файлу?

Давно видел в интернете туториал о том, как положить в tkinter.Entry путь к файлу, который выбрал, но не смог сейчас найти, и по этому решил воссоздать его сам, но у меня не получилось, помогите пожалуйста поправить мой код.
from tkinter import Button, Entry, StringVar, Tk
from tkinter.constants import E, END
from tkinter.filedialog import askopenfilename


class App(Tk):
    def __init__(self):
        super().__init__()
        self.resizable(False, False)
        self.title('InClass')

        #!org
        self.labelORG = Button(self, text='Предприятие')
        self.labelORG.grid(row=1, sticky=E)

        self.stringORG = StringVar()
        self.entryORG = Entry(self, textvariable=self.stringORG)
        self.entryORG.grid(row=1, column=1, columnspan=2)

        self.buttonORG = Button(self, text='Выбрать:', command=self._select_CSV_table(self.stringORG, self.entryORG))
        self.buttonORG.grid(row=1, col=3)

    def _select_CSV_table(path, entry_el):
        path = askopenfilename(filetype=(('CSV file', '*.csv'), ('Any', '*')))
        entry_el.delete(0, END)
        entry_el.insert(0, path)
        print(path)

if __name__ == '__main__':
    app = App()
    app.mainloop()
  • Вопрос задан
  • 407 просмотров
Решения вопроса 1
phaggi
@phaggi Куратор тега Python
лужу, паяю, ЭВМы починяю
Эээ... ну, я не вникал глубоко, как понял, так поправил.
код
from tkinter import Button, Entry, StringVar, Tk
from tkinter.constants import E, END
from tkinter.filedialog import askopenfilename


class App(Tk):
    def __init__(self):
        super().__init__()
        self.resizable(False, False)
        self.title('InClass')

        # !org
        self.labelORG = Button(self, text='Предприятие')
        self.labelORG.grid(row=1, sticky=E)

        self.stringORG = StringVar()
        self.entryORG = Entry(self, textvariable=self.stringORG)
        self.entryORG.grid(row=1, column=1, columnspan=2)

        self.buttonORG = Button(self, text='Выбрать:', command=self._select_CSV_table())  # , self.entryORG
        self.buttonORG.grid(row=1, column=3)

    def _select_CSV_table(self):
        my_path = askopenfilename(filetypes=(('CSV file', '*.csv'), ('Any', '*')))
        self.entryORG.delete(0, END)
        self.entryORG.insert(0, my_path)
        print(my_path)


if __name__ == '__main__':
    app = App()
    app.mainloop()

Это точно не "best practice", просто поправил, что попалось под руку.

p.s. 'path' - системная переменная, не надо ее переиспользовать.
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
Dmustache
@Dmustache Автор вопроса
Python, Cpp, SQL
Вот и сам ответ нашел на свой вопрос)
Вот, что я смог воссоздать:
command=lambda:self.stringORG.set(askopenfilename(filetypes=(('CSV file', '*.csv'))))
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы