如何将输入框(tkinter)中的文本指定给python脚本中的变量,并通过按下按钮运行脚本?

j2qf4p5b  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(382)

我有一个python脚本,它采用文件路径并运行以下脚本:

file = 'C:/Users/crist/Downloads/Fraction_Event_Report_Wednesday_June_16_2021_20_27_38.pdf'

    lines = []
    with pdfplumber.open(file) as pdf:
        pages = pdf.pages
        for page in pdf.pages:
            text = page.extract_text()
            print(text)

我用tkinter创建了一个输入框:

import tkinter as tk

master = tk.Tk()
tk.Label(master, 
         text="File_path").grid(row=0)

e = tk.Entry(master)

e.grid(row=0, column=1)

tk.Button(master, 
          text='Run Script', 
          command=master.quit).grid(row=3, 
                                    column=0, 
                                    sticky=tk.W, 
                                    pady=4)

tk.mainloop()

我想将用户在输入框中输入的文件路径指定给脚本中的“文件”,并在按下“运行脚本”按钮时运行脚本。我该怎么做?

g9icjywg

g9icjywg1#

最好生成一个文件对话框,而不是使用 tkinter.Entry :


# GUI.py

from tkinter.filedialog import askopenfilename
import tkinter as tk

# Create the window and hide it

root = tk.Tk()
root.withdraw()

# Now you are free to popup any dialog that you need

filetypes = (("PDF file", "*.pdf"), ("All files", "*.*"))
filepath = askopenfilename(filetypes=filetypes)

# Now use the filepath

lines = []
with pdfplumber.open(filepath) as pdf:
    ...

# Destroy the window

root.destroy()

相关问题