【问题标题】:Tkinter - how to pull error messages from cmd on screen?Tkinter - 如何从屏幕上的 cmd 提取错误消息?
【发布时间】:2023-03-25 01:23:01
【问题描述】:

我从我的 .py 项目创建了一个 .exe 文件。当我尝试在桌面上保存和输出 .csv 文件时,我得到“错误 13 权限被拒绝”,因为我没有管理员权限。相反,我需要将文件保存到 C:/Users/Public 以使其工作。 那么,每次我收到“错误 13”时,如何使用 Tkinter 制作带有此消息的弹出窗口?错误仅在 cmd 中显示。

## Write output to csv file
def save_file():
    path_to_output_file = fd.asksaveasfilename()
    if not path_to_output_file:
        return
    file_count = 1
    for rows in range(len(df)):
        if rows % 500 == 0:
            df[rows:rows+500].to_csv(path_to_output_file + str(file_count) + '.csv', index=False, header=True, quoting=csv.QUOTE_NONNUMERIC)
            file_count += 1

【问题讨论】:

  • 哪一行导致了错误?完整的错误信息是什么?您应该能够将违规行包含在 try: <line with error> except PermissionError: <code to display popup> 中。
  • Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):文件“tkinter_init_.py”,第 1892 行,在 call 文件“目录中Master.py”,第 136 行,在 save_file 文件中“pandas\core\generic.py”,第 3466 行,在 to_csv 文件中“pandas\io\formats\format.py”,第 1105 行,在 to_csv 文件中“pandas\io\格式\csvs.py",第 237 行,保存文件 "pandas\io\common.py",第 702 行,在 get_handle PermissionError: [Errno 13] Permission denied: 'C:/Users/Melleo/Desktop/dm1.csv '
  • 第136行,即:df[rows:rows+500].to_csv(path_to_output_file + str(file_count) + '.csv', index=False, header=True, quoting=csv.QUOTE_NONNUMERIC)

标签: python pandas tkinter permission-denied


【解决方案1】:

如果您已经收到错误消息,您可以使用tkinter.messagebox.showinfo 函数创建一个弹出窗口并显示错误消息。 你可以在这里找到文档: https://docs.python.org/3/library/tkinter.messagebox.html

【讨论】:

    【解决方案2】:

    谢谢! 成功了!

    try:
        df[rows:rows+500].to_csv(path_to_output_file + str(file_count) + '.csv', index=False, header=True, quoting=csv.QUOTE_NONNUMERIC)
    except PermissionError:
        messagebox.showinfo('Can\'t save a file', f'File can\'t be saved to {path_to_output_file} as you don\'t have admin privileges.\nYou need to save a file to \'C:/Users/Public\'')
    

    enter image description here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 2016-07-19
      • 2015-07-11
      • 2013-05-28
      相关资源
      最近更新 更多