You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

尝试在Tkinter窗口展示PDF失败:窗口未打开且无异常抛出

解决Tkinter无法打开PDF窗口的问题

核心问题:缺少主事件循环

你的代码创建了窗口但没有启动Tkinter的主事件循环,窗口会在创建后立即关闭,所以看不到任何界面,也不会抛出异常。

修复后的代码

import tkinter as tk
from tkPDFViewer import tkPDFViewer as pdf

cur_file='PPC_Bach.Eng_.-de-Computacao_AGES_Jacobina.pdf'

try:
    newWindow = tk.Tk()
    # 设置窗口标题(可选)
    newWindow.title("PDF Viewer")
    # 创建PDF视图并布局
    pdf_viewer = pdf.ShowPdf().pdf_view(newWindow, pdf_location=cur_file, width=75, height=100)
    pdf_viewer.pack(fill=tk.BOTH, expand=True)
    # 启动主事件循环
    newWindow.mainloop()
except Exception as e:
    print(e)

额外排查点

  • 确认cur_file路径正确:如果PDF文件不在脚本同目录下,需提供完整绝对路径,比如C:/docs/xxx.pdf(Windows)或/home/user/xxx.pdf(Linux/macOS)。
  • 检查依赖完整性:tkPDFViewer依赖PyMuPDF库,执行pip install PyMuPDF确保安装成功。
  • 调整窗口尺寸:如果显示不全,可在创建窗口后添加newWindow.geometry("800x600")设置初始大小。

内容的提问来源于stack exchange,提问作者Victor Luz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 15:45:54