0

I have a hard time using file handling when I combine my main function and the gui code. I am new to both OOP and GUI (TKinter). I want my program to have a button and will open a file. Then this file will be used in the main() function and then it does what is tasked to do. Here is a part of the code:

class GuiFrame1(Frame):
  def __init__ (self):
     Frame.__init__(self)
     self.master.geometry("500x500")
     self.pack(expand = 1, fill = BOTH)

     self.FileOpenerButton = Button(self,\
        text = "Open File", command = self.OpenFile)
     self.FileOpenerButton.pack()

  def OpenFile(self):
     inputFile = tkFileDialog.askopenfile()
     inputFile.close()

def main():
    with open ("LotData1.txt", "r") as fp:
    sideList = []
    for i in fp:
        tmp = i.strip().split()
        sideList.append([tmp[0], tmp[1], float(tmp[2])])

So it is like I want to remove the with open ("LotData1.txt", "r") as fp: and it shall use the file that I selected with the Gui. Here's the full code if someone is interested. http://pastebin.com/PGdXk8Bx

The reason why I used the with open function is because I tried to see if my main() function works and if it calculates the needed things for the output, and then I am trying to implement it with TKinter.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

3 Answers3

0

askopenfilename returns the path to the file. So I don't know why you use a close handle ? Does is make an error?

def OpenFile(self):
    inputFile = tkFileDialog.askopenfile()
    main(inputFile)

def main(Filename):
    with open (Filename, "r") as fp:

You can use class

user2740652
  • 361
  • 3
  • 12
  • `askopenfilename` returns a filename, but they aren't calling `askopenfilename`. The OP is calling `askopenfile`, which returns a file handle. – Bryan Oakley May 27 '15 at 13:48
  • sorry didn't saw that, use 'inputFile' has it is 'fp', and you can delete the 'with open' – user2740652 May 27 '15 at 13:50
0

The answer to your specific question about accessing a filename opened in a method of a class is to a) use getopenfilename rather than getopenfile, and b) saving the filename as an attribute on the class:

def OpenFile(self):
    self.inputFile = tkFileDialog.askopenfilename()

With that, anywhere else in your code you can use self.inputFile whenever you need the filename.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
-2

Answer for python 2.7

OOP lesson 1

There is a big difference between a class and an object. The best analogy I can think of is that the class is a recipe for a cake. The object is a cake.

objects and cakes

The logic of the cake analogy holds so far as you can have multiple instances of the objects build using the class. (multiple cakes, that are all made from the same recipe)

just like with objects, the cakes are not necessesarily equal after creation. (somebody ate some piece of my cake, but avoided yours, so the cakes are different).

base classes, inheritance (or using a standard recipe) say you want to bake a blueberry muffin. You dig into your books, and see a recipe that is written as follows:

mix together the standard muffin recipe on page 301, but leave out the icing. 
Add 100 blueberrys

This implies that a basic muffin recipe exists, and the author was to lazy to write all the common stuff twice.

When defining classes, "base classes" are like that. It is the definition of the standard muffin recipe. You can make your own version by inheriting from the base class.

OOP lesson 2

The cake is a lie, at least for python programming. We talk about classes inheriting from objects.. SO Question on that

So please don't try to learn "OOP" by learning python, as the pythonic way is .. well pythonic.

GUI

A Graphical User Interface GUI is just that. Nothing more and nothing else. A multitude of frameworks exists, TKinter is one of them, QT another, and Visual language is a third, where the last is a microsoft thing, and not python friendly.

Tkinter is ... special I would not recommend it for people trying to learn how to create GUI's. That being said, it can be quite flexible, and is shipped with python, so it's usable in some cases, and have a right to be there..

Hay don't get mad at me, I love Tkinter, but I've learned that most people would argue in favor of my previous comment.

Code specific Q and A

How do you intent to run anything? You are defining a class, and a function called "main" but this is not "C", so main is never actually run...

where do you intend to start the GUI, I can only see code for modifying a GUI, by packing in some stuff...

What was the program supposed to do? It sounds to me like you wan't to actually import the text in the file as python code, and run it?

Community
  • 1
  • 1
Henrik
  • 2,180
  • 16
  • 29
  • 1
    I think _'don't try to learn "OOP" by learning python'_ is terrible advice. Python is a great language for learning OOP. I also think _"I would not recommend it for people trying to learn how to create GUI's"_ is particularly terrible advice. Tkinter is an absolutely _fantastic_ way to learn about GUIs. There's very little overhead, and it supplies most of the basic features of all GUI toolkits. It's very easy to get started with tkinter, and there's a lot you can learn from it. – Bryan Oakley May 27 '15 at 14:00
  • I've been teaching it.. I found it easier to learn the kids C# or java first.. Hence my very personal comment – Henrik May 27 '15 at 14:01