python - 在 tkinter 中构建了一个简单的 GUI 提示用户输入值之后,您如何实际使用他们输入的值?
问题描述
我感谢你的耐心。我是 Python 和 Tkinter 的新手。
我有以下代码提示用户上传文件和赢率百分比。我在另一个文件中编写了一个脚本,该文件使用 pandas 对文件执行一系列操作。问题是我正在努力实际使用用户输入的值,并且目前不确定如何实际运行我在用户值上编写的脚本。我敢肯定这是一个非常简单的解决方案,但对于我的生活,我正在努力解决这个问题。一旦用户输入了他们的值并单击“开始转换”按钮,我将实际运行脚本。您可以提供的任何帮助将不胜感激,谢谢:)
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
window = Tk()
window.title("Pipeline tool")
window.geometry("250x170")
lbl = Label(window, text="UPLOAD", font=("Arial Bold", 15))
lbl3 = Label(window, text = "Please upload your files into the fields below, than hit convert.", font = ("Arial Bold ", 10))
plbl = Label(window, text="Revenue File", font=("Arial ", 10))
hclbl = Label(window, text="Win Rate Percentage", font=("Arial ", 10))
lbl.grid(column=10, row=0)
lbl3.grid(column=10, row = 10)
plbl.grid(column = 8, row = 20)
hclbl.grid(column = 8, row = 30)
window.geometry('500x500')
txt = Entry(window,width=100)
txt.grid(column=10, row=20)
txt = Entry(window,width=100)
txt.grid(column=10, row=30)
#files = filedialog.askopenfilenames()
def clicked():
files = filedialog.askopenfilenames()
messagebox.showinfo('Successful Upload!', 'Your file has been successfully uploaded!')
def clicked1():
messagebox.showinfo('Please wait', 'Your files are being read. The output should download shortly. !')
btn = Button(window, text="Start Conversion", command= clicked1)
btn.grid(column=10, row=40)
btn = Button(window, text="UPLOAD", command= clicked)
btn.grid(column=13, row=20)
btn = Button(window, text="ENTER", command= clicked)
btn.grid(column=13, row=30)
window.mainloop()
解决方案
要存储文件路径,请创建一个全局变量(我称之为userFiles
)以在用户单击上传时存储文件。要获取赢率输入框中的值,请使用entry.get()
。我还为您的代码添加了一些验证,因此用户必须在进行计算之前上传文件并给出胜率。这是代码:
window.title("Pipeline tool")
window.geometry("250x170")
userFiles = None
lbl = Label(window, text="UPLOAD", font=("Arial Bold", 15))
lbl3 = Label(window, text = "Please upload your files into the fields below, than hit convert.", font = ("Arial Bold ", 10))
plbl = Label(window, text="Revenue File", font=("Arial ", 10))
hclbl = Label(window, text="Win Rate Percentage", font=("Arial ", 10))
lbl.grid(column=10, row=0)
lbl3.grid(column=10, row = 10)
plbl.grid(column = 8, row = 20)
hclbl.grid(column = 8, row = 30)
window.geometry('500x500')
fileEntry = Entry(window,width=100)
fileEntry.grid(column=10, row=20)
winEntry = Entry(window,width=100)
winEntry.grid(column=10, row=30)
#files = filedialog.askopenfilenames()
def clicked():
files = filedialog.askopenfilenames()
if files:
global userFiles
userFiles = files
messagebox.showinfo('Successful Upload!', 'Your file has been successfully uploaded!')
def clicked1():
global userFiles
winRate = winEntry.get()
if winRate != "" and userFiles != "" and userFiles != None:
print(winRate, userFiles)
#Do your calulations here...
messagebox.showinfo('Please wait', 'Your files are being read. The output should download shortly. !')
else:
messagebox.showwarning("Error","Please fill all fields in")
btn = Button(window, text="Start Conversion", command= clicked1)
btn.grid(column=10, row=40)
btn = Button(window, text="UPLOAD", command= clicked)
btn.grid(column=13, row=20)
btn = Button(window, text="ENTER", command= clicked)
btn.grid(column=13, row=30)
推荐阅读
- sql - SQL Report - query multiple tables
- symfony - Symfony 4 所有视图中的公共数据 - 最佳实践
- apache - Kylin 如何在 Hbase 中存储数据?
- python - 使用 ActionChains 在 Selenium Webdriver 中按 ctrl+t 不起作用
- regex - 正则表达式 - 在特定字符后查找第一个空格
- react-native - 错误:不支持同步 http 请求
- c++ - 从 .txt 文件中提取信息的最佳方式
- intellij-idea - 在 IntelliJ 的多个 .feature 文件中重构 Cucumber 步骤定义的名称
- python - AWS Lambda 中的 get_remaining_time_in_millis 有什么替代方法可用于 Google Cloud Function?
- c++ - 是否有理由使用 std::conjunction/std::disjunction 而不是“&&”/“||”上的折叠表达式?