Search code examples
pythonpython-3.xtkinterwxpythonwxwidgets

Tkinter GUI become unresponsive after clicking button to view pdf


Tkinter Issue In my current project, I am generating pdf file using reportlab and viewing it with pdf viewer which is coded using wxPython package. When i click button to view pdf the viewer got open and work properly but the GUI become unresponsive and button got freeze. I have to kill this process with task-Manager.

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")

This code is creating pdf file using header and footer image and fetching data from database.

    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)

This code is written to view the PDF file in a wx pdf viewer

    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()

When i click this button then this gui become unresponsive and i have to kill this process from task manager please help me with this.

    B2 = tkinter.Button(window, text="Preview and Print", width=14,
                        command=prev123)
    B2.place(x=570, y=95)

    window.mainloop()


job_folder()

Solution

  • Tkinter and wxPython are not compatible. They have their own event loops which will conflict with each other. If you need to use wxPython's PDF viewing capability, then you should write your application in wxPython, not in Tkinter.