python - 如何将函数的路径分配给使用 openpyxl 创建的 excel?
问题描述
我是 Python 新手,我一直在尝试创建一个函数“save_file()”;它为我提供了保存 Excel 文件的路径,Excel 文件是使用 openpyxl 从列表中创建的,例如:
import openpyxl as opxl
import tkinter as tk
wbk = opxl.Workbook()
worksheet1 = wbk.active
worksheet1.title = 'Species_concentration'
r = 2
for Hyd in Hyd_conc_list:
worksheet1.cell(row = r, column = 1).value = Hyd
r += 1
问题是,一旦创建了 Excel,我想将函数“save_file()”作为 tkinter 界面中的命令分配给一个按钮。我这样做了:
def save_file():
ftype = [('Excel file', '*.xlsx')]
idir = os.path.join (os.environ ['USERPROFILE'], 'Documents')
path_name = tk.filedialog.asksaveasfilename (filetypes = ftype, initialdir = idir)
wbk.save(path_name)
button3 = tk.Button(wdw, text = 'SAVE', bg = 'gray14', bd = 4, fg = 'white', relief = 'raised', command = save_file)
但它不起作用,我将不胜感激,非常感谢。
解决方案
推荐阅读
- javascript - 如何使用正则表达式获取单位?
- wpf - 如何根据是否等于另一个标签来更改WPF中标签的背景颜色?
- python-3.x - 如果两个字符串之间有一个公共子字符串,则为程序显示一些运行时错误,该错误会打印 YES
- sql - 用于将数据从数据库复制到另一个数据库的 SQL Server 作业
- reactjs - 即使 flex: 1 反应本机文本输入字段也没有占用全宽
- visual-studio-code - 如何使用 VSCode 将一行的第一个字符转换为大写?
- javascript - 有什么方法可以将普通对象转换为类对象?
- kubernetes - 限制其他命名空间访问 pod
- pandas - 当句子包含特殊字符时删除停用词
- html - 视频不自动播放html