Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

python - 试图从Python 3.6中的filedialog中通过按钮获取所选文件的完整路径(Trying to get full path of a chosen file by button from filedialog in Python 3.6)

Typing a GUI for further merging it with another part of code.

(键入GUI以进一步将其与另一部分代码合并。)

I have stuck with getting full path of a file like "x:/folder1/folder2/Sample.rar" as a string.

(我一直坚持以字符串形式获取“ x:/folder1/folder2/Sample.rar”之类的文件的完整路径。)

I need it to place into a Label or Entry (whatever, just to show it fetched) and using this string for further input that will do some work.

(我需要将其放入Label或Entry(无论如何,只是为了显示它已被提取),并使用此字符串作为进一步的输入来完成一些工作。)

I have tried to get is as a str but didn't get any result.

(我试图得到的是str,但没有得到任何结果。)

Looking forward to learn a way to make a string.

(期待学习制作琴弦的方法。)

import os
#import rarfile
import tkinter.messagebox
from tkinter import filedialog
from tkinter.filedialog import askopenfilename
from tkinter import *
from tkinter import ttk
from cgitb import text

window = Tk()
window.title("GUI")
window.geometry('430x500')

lbl3 = Label(window, text="Choose log archives: ", font='Trebushet 8')
lbl3.grid(column=0, row=10)

browseFile = Button(text="Browse files", font='Trebushet 8', width=15, command=lambda : 
            filedialog.askopenfilename(initialdir = "/",
            title = "Seledct archives including logs",
            filetypes = (("RAR files","*.rar"))))
browseFile.grid(column=0, row=11)

FileName = Label(window)
FileName.grid(column=0, row=12)

if browseFile:
    print (str(FileName))
window.mainloop()

Copy comment :I have found an approach but Label is not callable.

(复制评论 :我找到了一种方法,但Label无法调用。)

 def print_path(): f = tkinter.filedialog.askopenfilename( parent=window, initialdir='C:/', title='Choose file', filetypes=[("RAR files","*.rar"), ("all files","*.*")] ) print(str(FileName())) browseFile = tkinter.Button(window, text='Browse file', command=print_path) browseFile.grid(column=0, row=11) browseFile.pack(fill=FileName()) 
  ask by Alexander Larionov translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You forgot to add a , to the filetypes arg, see below.

(您忘记将,添加到文件类型arg,请参见下文。)

filetypes = (("RAR files","*.rar"),)))

(文件类型=(((“ RAR文件”,“ *。rar”),))))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...