Tkinter问题在我当前的项目中,我正在使用reportlab生成pdf文件,并使用pdf查看器进行查看,该查看器是使用wxPython软件包编码的。当我单击按钮查看pdf时,查看器打开并正常工作,但GUI变得无响应,按钮被冻结。我必须使用任务管理器终止此过程。
import tkinter
from tkinter import ttk
import storage
from tkinter import messagebox
from tabulate import tabulate
from tkcalendar import Calendar, DateEntry
import wx
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4, inch
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle,
Paragraph, Image, Spacer
from reportlab.lib.styles import getSampleStyleSheet
import locale
import wx.lib.sized_controls as sc
from wx.lib.pdfviewer import pdfViewer, pdfButtonPanel
def job_folder():
window = tkinter.Tk()
window.geometry("1200x550+100+50")
window.resizable(False, False)
window.title("Job Folder")
此代码使用页眉和页脚图像创建pdf文件,并从数据库中获取数据。
app = wx.App()
frame = wx.Frame(None, title='Simple application')
doc = SimpleDocTemplate("job_folder1.pdf", pagesize=A4, rightMargin=30, leftMargin=30, topMargin=30,
bottomMargin=18)
doc.pagesize = A4
elements = []
logo = "header.jpg"
im = Image(logo, 7 * inch, 1.1 * inch, hAlign='CENTER')
elements.append(im)
elements.append(Spacer(1, 0.25 * inch))
db = storage.connect()
cursor = db.cursor()
cursor.execute(
"select CONCAT(prefix,job_no ) AS job,date_,account,shipper,
consignee, maste, house, freight, place_receipt, final_dest, status
"
" from sea_exp_tra_job_folder ")
data = cursor.fetchall()
db.close()
dataa = [('Job No', 'Date', 'Account', 'Shipper', 'Consignee', 'Master',
'House', 'PP/CC', 'Origin', 'Destination', 'Status')]
dataa.extend(data)
data = tuple(dataa)
style = TableStyle([('ALIGN', (1, 1), (-2, -2), 'RIGHT'),
('BACKGROUND', (0, 0), (-1, 0), colors.powderblue),
('TEXTCOLOR', (1, 1), (-2, -2), colors.black),
('VALIGN', (0, 0), (0, -1), 'TOP'),
('TEXTCOLOR', (0, 0), (0, -1), colors.black),
('ALIGN', (0, -1), (-1, -1), 'CENTER'),
('VALIGN', (0, -1), (-1, -1), 'MIDDLE'),
('TEXTCOLOR', (0, -1), (-1, -1), colors.green),
('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
('BOX', (0, 0), (-1, -1), 0.25, colors.black),
])
s = getSampleStyleSheet()
s = s["BodyText"]
s.wordWrap = 'CJK'
data2 = [[Paragraph(cell, s) for cell in row] for row in data]
t = Table(data2)
t.setStyle(style)
elements.append(t)
elements.append(Spacer(1, 0.25 * inch))
logo1 = "footer.png"
im1 = Image(logo1, 6 * inch, 0.6 * inch, hAlign='CENTER')
elements.append(im1)
doc.build(elements)
编写此代码是为了在wx pdf查看器中查看PDF文件
def prev123():
locale.setlocale(locale.LC_ALL, 'C')
class PDFViewer(sc.SizedFrame):
def __init__(self, parent, **kwds):
super(PDFViewer, self).__init__(parent, **kwds)
paneCont = self.GetContentsPane()
self.buttonpanel = pdfButtonPanel(paneCont, wx.ID_ANY,
wx.DefaultPosition,
wx.DefaultSize, 0)
self.buttonpanel.SetSizerProps(expand=True)
self.viewer = pdfViewer(paneCont, wx.ID_ANY,
wx.DefaultPosition,
wx.DefaultSize,
wx.HSCROLL | wx.VSCROLL |
wx.SUNKEN_BORDER)
self.viewer.SetSizerProps(expand=True, proportion=1)
self.buttonpanel.viewer = self.viewer
self.viewer.buttonpanel = self.buttonpanel
if __name__ == '__main__':
import wx.lib.mixins.inspection as WIT
app = WIT.InspectableApp(redirect=False)
pdfV = PDFViewer(None, size=(800, 600))
pdfV.viewer.LoadFile(r'job_folder1.pdf')
pdfV.Show()
app.MainLoop()
当我单击此按钮时,此gui变得无响应,因此我必须从任务管理器中终止此过程,请为此提供帮助。
B2 = tkinter.Button(window, text="Preview and Print", width=14,
command=prev123)
B2.place(x=570, y=95)
window.mainloop()
job_folder()
答案 0 :(得分:0)
Tkinter和wxPython不兼容。它们具有自己的事件循环,它们将相互冲突。如果需要使用wxPython的PDF查看功能,则应使用wxPython而不是Tkinter编写应用程序。