Here's my code:
from Tkinter import *
import tkFileDialog
import ImageTk
import Image
class GUI(Tk):
def __init__(self, master = None):
## self.apfr = Frame()
## self.before = self.graphicWindow("Input")
## self.after = self.graphicWindow("Output")
##
## self.apfr.icon = Button(self.apfr, text = "Minimize",
## command = self.ico)
##
## self.apfr.quit = Button(self.apfr, text = "Exit",
## command = self.destroy)
## self.apfr.open = Button(self.apfr, text = "Open",
## command = lambda: self.opfunc(self.before))
##
## self.show(self.apfr, 0, 2)
## self.show(self.apfr.open, 0, 0)
## self.show(self.apfr.icon, 0, 1)
## self.show(self.apfr.quit,0,2)
pass
def show(self, obj, r, c):
obj.grid(row = r, column = c, sticky = N+S+E+W)
obj.rowconfigure(0,weight = 1)
obj.columnconfigure(0,weight = 1)
def graphicWindow(self, title):
self.window = Toplevel()
self.window.canvas = Canvas(width = 600, height = 400,
scrollregion = (0,0,600,400))
self.window.scrollY = Scrollbar(orient= VERTICAL,
command = self.window.canvas.yview)
self.window.scrollX = Scrollbar(orient = HORIZONTAL,
command = self.window.canvas.xview)
self.window.canvas.grid(row = 0, column =0, sticky = N+S+E+W)
self.window.canvas.rowconfigure(0,weight = 1)
self.window.canvas.columnconfigure(0,weight = 1)
self.window.scrollY.grid(row=0, column=1, sticky = N+S)
self.window.scrollX.grid(row=1, column=0, sticky = E+W)
self.window.canvas["xscrollcommand"] = self.window.scrollX.set
self.window.canvas["yscrollcommand"] = self.window.scrollY.set
return self.window
def ico(self):
self.before.iconify()
self.after.iconify()
self.iconify()
def opfunc(self, arg):
arg.file = tkFileDialog.askopenfilename(parent=top,initialdir="/",
title='Please select a file to open',
filetypes = [("JPEG", ".jpg"),
("GIF", ".gif"), ("Bitmap", ".bmp")])
arg.img = Image.open(self.file)
arg.dat = ImageTk.PhotoImage(self.img)
arg.graphic = self.canvas.create_image(0,0,anchor = "nw", image = self.dat)
arg.title(self.file)
baah = GUI()
baah.mainloop()
So I started learning pythons yesterday and found it incredibly fun to program... until I try to write it in an OOP form. I've had SO much issues with scope of variables and functions, now the current problem is that the code runs into an infinite loop error. I've tried searching google and found one similar problem but no cigar. I'm not sure where the problem lies but here's the error:
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1721, in __getattr__
return getattr(self.tk, attr)
and it repeats until it reaches max recursion length.
Any help with this program and advice with the language's function and scope in general would be greatly appreciated. I can't go on to make this program useful until I at least have a container for it, and I sure hope someone could tell me an easier way to implement the function definitions because I'm sick of writing "self".
THANKS!
Editor's note:
Please use the [code=python] and [/code] tag pair to enclose your python code.