0
votes

I am trying to make an interactive graph that is displayed in the Tkinter window. However, I am not able to create a Frame object to place in the Grid of the Tkinter window.

I have created my Tkinter object in a class style here is the instantiation of it

window = Tk()
w = Graph(window, size, startX, startY, endX, endY)
window.geometry('1000x1000')
window.title('Tkinter Testing')
window.mainloop()

I am referring to this tutorial: Tkinter Geometry Manager

for x in range(len(self.graph)):
    for y in range(len(self.graph[0])):
        frame = window.Frame(
            master=window,
            relief=window.RAISED,
            borderwidth=1
        )
        frame.grid(row=x,column=y)
        label = window.Label(master = frame, text="testing")

This is the error thrown in the console

DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning.
Traceback (most recent call last):
  File "Graph.py", line 158, in <module>
    w = Graph(window, size, startX, startY, endX, endY)
  File "Graph.py", line 23, in __init__
    self.displayGraph()
  File "Graph.py", line 39, in displayGraph
    frame = window.Frame(
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1909, in __getattr__
    return getattr(self.tk, attr)
AttributeError: Frame
1
Did you mean ‘Frame(...’ or ‘tk.Frame(...’? - quamrana
Also, you're calling .grid() on the window instead of the Frame, so none of your Frames will actually appear. - jasonharper
If you did import tkinter as tk, you could say things like tk.Tk() or tk.Label(). If you did from tkinter import *, you would instead have to say Tk() or Label() (this option is generally not recommended, since it's no longer obvious where the definitions of these names are coming from). - jasonharper
I doubt that frame = window.tk.Frame() throws exactly the same error. Anyway, did you try frame = Frame(...? - quamrana
Err... @jsonharper has already explained all this. Perhaps you need to update the question with your import statements for tkinter. - quamrana

1 Answers

0
votes

Credit goes to @quamarana and @jasonharper for shedding light and explaining the issue.

The answer to this question had to do with how Tkinter is imported. The syntax when instantiating an object is slightly different based on the import statement.

import tkinter as tk or from tkinter import Tk

button = tk.Button(...
frame = tk.Frame(...
label = tk.Label(...

from tkinter import *

button = Button(...
frame = Frame(...
label = Label(...

As @jasonharper pointed out:

If you did from tkinter import *, you would instead have to say Tk() or Label() (this option is generally not recommended, since it's no longer obvious where the definitions of these names are coming from).