Python Forum
class returns NoneType Object
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
class returns NoneType Object
#1
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(self.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 rally familiar with OOP so am currently learning but as I understand, the program calls the class as expected,
app = SampleApp()
reads the file correctly and opens the file 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 (because it happens under gui.py). 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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in class: TypeError: 'str' object is not callable akbarza 2 418 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 642 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 889 Aug-24-2023, 05:14 PM
Last Post: snippsat
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,322 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  TypeError: 'NoneType' object is not subscriptable syafiq14 3 5,135 Sep-19-2022, 02:43 PM
Last Post: Larz60+
  AttributeError: 'NoneType' object has no attribute 'group' MaartenRo 3 4,561 Jan-02-2022, 09:53 AM
Last Post: snippsat
  AttributeError: 'NoneType' object has no attribute 'group' MaartenRo 3 2,509 Jan-01-2022, 04:16 PM
Last Post: MaartenRo
  Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS Franky77 2 5,127 Aug-17-2021, 05:24 PM
Last Post: Franky77
Exclamation win32com: How to pass a reference object into a COM server class Alfalfa 3 4,767 Jul-26-2021, 06:25 PM
Last Post: Alfalfa
  AttributeError class object has no attribute list object scttfnch 5 3,300 Feb-24-2021, 10:03 PM
Last Post: scttfnch

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020