2 Replies - 177 Views - Last Post: 12 August 2019 - 05:25 AM Rate Topic: -----

#1 istemihan90   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 4
  • Joined: 28-July 19

class returns NoneType Value

Posted 12 August 2019 - 04:31 AM

Hello, I am trying to read PDF file in Gui class and then I will use that read file to do some graphical process.

Here is the Gui class:
Gui.py:
# Multi-frame tkinter application v2.3
import os
import sys
sys.path.insert(0, "..")
import tkinter as tk
import tkinter.filedialog


class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.switch_frame(StartPage)
        self.title("Welcome to EMC Deep Learning Application")
        self.geometry('1000x500')
        self.pdf_File = None

    def switch_frame(self, frame_class):
        """Destroys current frame and replaces it with a new one."""
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.grid()

##    def on_change(e):
##        print e.widget.get()



class StartPage(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Label(self, text="Ne işlem yapılacak?").grid(row=1,column=1)
        tk.Button(self, text="Öğret",
                          command=lambda: master.switch_frame(PageOne)).grid()
        tk.Button(self, text="Öğren",
                          command=lambda: master.switch_frame(PageTwo)).grid()

class PageOne(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Label(self, text="Öğretmek için orjinal test sonuçlarının yüklü olduundan emin olun").grid(row=1,column=1)
        tk.Button(self, text="Ana Sayfaya Dön",
                  command=lambda: master.switch_frame(StartPage)).grid(row=2,column=1)
        
        vendorVar = tk.StringVar(self) #create a panel vendor variable
        vendors = {'BOE','SAMSUNG','LG'}
        vendorVar.set('BOE') # set default opt
        self.vendorVar = vendorVar
        vendorLabel = tk.Label(self, text="Marka:")
        vendorLabel.grid(row = 3, column = 1)
        popMenuVendor = tk.OptionMenu(self, vendorVar, *vendors)
        popMenuVendor.grid(row=4,column=1)

        techVar = tk.StringVar(self) #create a panel vendor variable
        techs = {'LCD','OLED','QDOT'}
        techVar.set('LCD') # set default opt
        self.techVar = techVar
        techLabel = tk.Label(self, text="Teknoloji:")
        techLabel.grid(row = 3, column = 2)
        popMenuTech = tk.OptionMenu(self, techVar, *techs)
        popMenuTech.grid(row=4,column=2)

        sizeBox = tk.IntVar(self)
        tk.Label(self, text="Ekran Boyutu:").grid(row = 3, column = 3)
        sizeBox = tk.Entry(self)
        sizeBox.grid(row=4,column=3)
        self.sizeBox = sizeBox

        tk.Label(self, text="PDF dosyası").grid(row = 3, column = 4)
        pdfButton = tk.Button(self, text="Yükle",
                  command=lambda: self.pdfUpload())
        pdfButton.grid(row=4,column=4)
        
        buttonT = tk.Button(self, text="Goster", command=self.ok)
        buttonT.grid(row=5,column=1)
    def ok(self):
        print ("vendor is: " + self.vendorVar.get() )
        print("tech is " +  self.techVar.get() )
        if(self.sizeBox.get().isdigit()):
            print("size is: " + self.sizeBox.get())
        else:
            print("not an integer!")
        master.pdf_File = self.pdf_File

    def pdfUpload(self):
        self.pdf_File = tk.filedialog.askopenfilename(initialdir = "/Users/26015017/Desktop/",title = "Dosya Seçin",filetypes = [("pdf files","*.pdf")])
##        os.startfile(master.pdf_File)
##        master.quit()
        
class PageTwo(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        tk.Label(self, text="This is page two").pack(side="top", fill="x", pady=10)
        tk.Button(self, text="Return to start page",
                  command=lambda: master.switch_frame(StartPage)).pack()

if __name__ == "__main__":
    app = SampleApp()
    
    app.mainloop()



and here is the main file:
TestBench.py:
import cv2
import tkinter
import numpy as np
import matplotlib.pylab as plt
import matplotlib.pyplot as plot 
'exec(%matplotlib inline)'
import tabula
from classes.Graphs import *
from classes.Gui import *

from pdf2image import convert_from_path
from pdf2image import convert_from_bytes
##tkinter._test()

app = SampleApp()
    
app.mainloop()

pdf_File = app.pdf_File
os.startfile(pdf_File)
##pdf2PNG = convert_from_path('images/ex2.Pdf', 500)
pdf_File.save('images/temp/ex2.pdf', 'PDF')
##pdf2PNG = convert_from_bytes(app.pdf_File, 500)

tabula.convert_into("images/ex2.pdf", "images/ex2.csv", output_format="csv", pages='all')
tabula.convert_into(pdf2PNG, "images/ex2.csv", output_format="csv", pages='all')

pdf2PNG[0].save('images/temp/ex2.png', 'PNG')

img = cv2.imread('images/temp/ex2.png')




graph1 = BasicGraph(1, img)

print("Graph revision: ", graph1.revision)
print("Y origin is: ", graph1.yOrigin)
print("xAxis legth is: ", graph1.xAxisLength)
print("X origin is: ", graph1.xOrigin)
print("yAxis legth is: ", graph1.yAxisLength)
print("47 Limit Len: ", graph1.limit47Length)
print("47 Limit Y is: ", graph1.y47)
print("40 Limit Len is: ", graph1.limit40Length)
print("40 Limit Y is: ", graph1.y40)
print("Last data on X axis is: ", graph1.lastDataX)
print("Last y axis is: ", graph1.lastXAxis)
print("unitY is : ", graph1.unitY)


plot.plot(graph1.hrzData)
plot.plot(graph1.vrtData)
plot.axis([0, 970, 0, 80])
plot.show()

cv2.waitKey(0)
cv2.destroyAllWindows()



what I get (after selecting the file) is like this:

Quote

Traceback (most recent call last):
File "D:\Users\26015017\Desktop\Deneme9\testBench_2.py", line 20, in <module>
os.startfile(pdf_File)
TypeError: startfile: filepath should be string, bytes or os.PathLike, not NoneType
>>>


I am not eally familiar with OOP currently learning but as I understand, the programm calls the class as expected,
app = SampleApp()


reads the file correctly and opens the flie after reading correctly in class script:

    def pdfUpload(self):
        self.pdf_File = tk.filedialog.askopenfilename(initialdir = "/Users/26015017/Desktop/",title = "Dosya Seçin",filetypes = [("pdf files","*.pdf")])
        os.startfile(self.pdf_File
)

When I run os.startfile I see the document I choosed. And till I close the window, I don't get the warning so I think after I close the window, all the variables in the class vanishes too. So my question is 'what is happening here?' If I correctly assessed the problem, how can I overcome this problem?

Is This A Good Question/Topic? 1
  • +

Replies To: class returns NoneType Value

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,229
  • Joined: 12-December 12

Re: class returns NoneType Value

Posted 12 August 2019 - 05:18 AM

Break it down with some debugging steps and print statements.

After line 19, pdf_File = app.pdf_File, print out the value of pdf_File to see what it contains (if anything). The error suggests that it contains nothing (NoneType).

Just after def pdfUpload(self): write a simple message to confirm that this method is called. Then, just after self.pdf_File, print this value to see what it contains.
Was This Post Helpful? 0
  • +
  • -

#3 istemihan90   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 4
  • Joined: 28-July 19

Re: class returns NoneType Value

Posted 12 August 2019 - 05:25 AM

View Postandrewsw, on 12 August 2019 - 05:18 AM, said:

Break it down with some debugging steps and print statements.

After line 19, pdf_File = app.pdf_File, print out the value of pdf_File to see what it contains (if anything). The error suggests that it contains nothing (NoneType).

Just after def pdfUpload(self): write a simple message to confirm that this method is called. Then, just after self.pdf_File, print this value to see what it contains.



Thank you Sir! I did it. Message is produced then I read the object:

    def pdfUpload(self):
        self.pdf_File = tk.filedialog.askopenfilename(initialdir = "/Users/26015017/Desktop/",title = "Dosya Seçin",filetypes = [("pdf files","*.pdf")])
        print("oldu!")
        print("PDF file contains: " + self.pdf_File)
        pdf_g_f = self.pdf_File


This is the output.

Quote

oldu!
PDF file contains: D:/Users/26015017/Desktop/Deneme9/images/example.Pdf


it seems that the object contains the address of the file. But I am not sure what I can do with this? Can't I hold the object as the file itself?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1