如何通过按下按钮将输入框 (tkinter) 中输入的文本分配给 python 脚本和 运行 脚本中的变量?
How can I assign the text imputed into an entry box (tkinter) to variable in python script and run the script by pressing a button?
我有一个采用文件路径的 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()
我想将用户在输入框中输入的 File_path 分配给脚本中的“文件”,并在按下“运行 脚本时将 运行 分配给脚本“ 按钮。我该怎么做?
生成文件对话框比使用 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()
我有一个采用文件路径的 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()
我想将用户在输入框中输入的 File_path 分配给脚本中的“文件”,并在按下“运行 脚本时将 运行 分配给脚本“ 按钮。我该怎么做?
生成文件对话框比使用 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()