Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Imrpovement #5

Open
RobinDemay opened this issue Oct 14, 2021 · 1 comment
Open

Imrpovement #5

RobinDemay opened this issue Oct 14, 2021 · 1 comment

Comments

@RobinDemay
Copy link

RobinDemay commented Oct 14, 2021

Hello,
I would like to offer you some improvements

`

class ShowPdf(threading.Thread, Frame):

img_object_li = []

def __init__(self, parent, width=1200, height=600, pdf_location="", bar=True, load="after"):
    threading.Thread.__init__(self)
    Frame.__init__(self, parent, width=width, height=height, bg="white")

    self.__flagKill = False
    self.__flagRefresh = True

    self.pdf_location = pdf_location
    self.bar = bar
    self.load = load

    scroll_y = Scrollbar(self, orient="vertical")
    scroll_x = Scrollbar(self, orient="horizontal")

    scroll_x.pack(fill="x", side="bottom")
    scroll_y.pack(fill="y", side="right")

    self.percentage_load = StringVar()

    if self.bar == True and self.load == "after":
        self.display_msg = Label(self, textvariable=self.percentage_load)
        self.display_msg.pack(pady=10)

        self.loading = Progressbar(self, orient=HORIZONTAL, length=100, mode='determinate')
        self.loading.pack(side=TOP, fill=X)

    self.text = Text(self, yscrollcommand=scroll_y.set, xscrollcommand=scroll_x.set, width=width, height=height)
    self.text.pack(side="left")

    scroll_x.config(command=self.text.xview)
    scroll_y.config(command=self.text.yview)

def run(self):
    while self.__flagKill is False:
        if self.__flagRefresh is True:
            precentage_dicide = 0

            with fitz.open(self.pdf_location) as open_pdf:
                for page in open_pdf:
                    pix = page.getPixmap()
                    pix1 = fitz.Pixmap(pix, 0) if pix.alpha else pix
                    img = pix1.getImageData("ppm")
                    timg = PhotoImage(data=img)
                    self.img_object_li.append(timg)
                    if self.bar == True and self.load == "after":
                        precentage_dicide = precentage_dicide + 1
                        percentage_view = (float(precentage_dicide)/float(len(open_pdf))*float(100))
                        self.loading['value'] = percentage_view
                        self.percentage_load.set(f"Please wait!, your pdf is loading {int(math.floor(percentage_view))}%")
                if self.bar == True and self.load == "after":
                    self.loading.pack_forget()
                    self.display_msg.pack_forget()

            index = 1.0
            self.text.configure(state='normal')
            for i in self.img_object_li:
                self.text.image_create(index, image=i)
                self.text.insert(index, "\n\n")
                self.text.image = i  #Very important, otherwise the thread gets blocked
                index += 1
            self.text.configure(state="disabled")
            self.img_object_li.clear()  #Avoid duplicates
            self.__flagRefresh = False

def refresh(self):
    self.__flagRefresh = True

def stop(self):
    self.__flagKill = True

`

If you want to add a pdf afterwards, you have to make these modifications:

`

            self.text.configure(state='normal')
            for i in self.img_object_li:
                self.text.image_create(END, image=i)
                self.text.insert(END, "\n\n")
                self.text.image = i  #Very important, otherwise the thread gets blocked
            self.text.configure(state="disabled")
            self.img_object_li.clear()  #Avoid duplicates
            self.__flagRefresh = False

def add_pdf(self, pdf_location):
    self.pdf_location = pdf_location
    self.__flagRefresh = True

`

Example of use:

`

self.pdfView = obdoTkShowPDF(self, pdf_location=pathPDF, width=50, height=30)
self.pdfView.grid(row=1, column=1)
self.pdfView.start()
#do stuff
self.pdfView.stop()
self.pdfView.join()

`

@CodeYan01
Copy link

This. Inheriting from Frame is better as you do not have to chain calls unnecessarily, like pdfView = pdf.ShowPdf().pdf_view(), which has no benefit. The ___init__() exists for a reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants