首页 > 解决方案 > 如何获取用户选择的文件并将其提供给 Tkinter 中的 python 程序

问题描述

我目前正在为 Tkinter 为我创建的项目创建一个界面而烦恼。该程序接受一堆文件路径作为其运行的输入。我正在尝试创建一个 tkinter 界面,我可以在其中上传我需要的 4 个文件,或者至少以某种方式获取文件路径和 . 将它们提供给程序。这是我所拥有的:

import sys
import os
import comparatorclass
from tkinter import *
from tkinter.ttk import *
from tkinter.filedialog import askopenfile 



root=Tk()
root.geometry('1000x1000')  


def open_file(): 
    file = askopenfile(mode ='r', filetypes =[('Python Files', '*.py')]) 
    if file is not None: 
        content = file.read() 
        print(content) 

def run_comparator():
    comparatorclass.main()
    

button2 = Button(root, text ='Open', command = lambda:open_file()) 
button2.pack(side = TOP, pady = 10) 

button1 = Button(root,text="hello",command= run_comparator)
button1.pack()




root.mainloop()

如您所见,我有两个按钮。我遇到的问题是如何将我的 openfile 函数连接到我的 run_comparator 函数,以便将我需要打开的 4 个文件传递给 run_comparator

标签: pythonfileuser-interfacetkintertkinter-layout

解决方案


推荐阅读