Home>
IDLE cannot resolve the "root.geometry" error.

I'm new to Python.
I'm writing the code based on a reference book,
The following error message has occurred.
I don't know how to deal with it, so I would appreciate it if you could ask.

Traceback (most recent call last):
  File "C: /Users/ken/Desktop/de01.py", line 21, in<module>
    root.geometry ("200x150")
  File "C: \ Program Files \ WindowsApps \ PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0 \ lib \ tkinter \ __ init__.py", line 2035, in wm_geometry
    return self.tk.call ('wm','geometry', self._w, newGeometry)
AttributeError:'str' object has no attribute'tk'
Corresponding source code
import tkinter as tk
import tkinter.filedialog as fd
import PIL.Image
import PIL.ImageTk
def dispPhoto (path):
    #Load image
    newImage = PTL.Image.open (path) .resize ((300,300))
    #Display the image on the label
    imageData = PIL.ImageTK.PhotoImage (newImege)
    imageLabel.configure (image = imageData)
    imageLabel.image = imageData
def openFile ():
    fpath = fd.askopenfilename ()
    if fpath:
        dispPhoto (fpath)
root = tk.TK
root.geometry ("200x150")
btn = tk.Button (text = "open file", command = openFile)
imageLabel = tk.Label ()
btn.pack ()
imageLabel.pack ()
tkmainloop ()
Supplementary information (FW/tool version, etc.)

windouws10
Python 3.8.3

  • Answer # 1

    For the time being

    *** 17,27 ****
          if fpath:
              dispPhoto (fpath)
    ! root = tk.TK
      root.geometry ("200x150")
      btn = tk.Button (text = "open file", command = openFile)
      imageLabel = tk.Label ()
      btn.pack ()
      imageLabel.pack ()
    ! tkmainloop ()
    --- 17,27 ----
          if fpath:
              dispPhoto (fpath)
    ! root = tk.Tk ()
      root.geometry ("200x150")
      btn = tk.Button (text = "open file", command = openFile)
      imageLabel = tk.Label ()
      btn.pack ()
      imageLabel.pack ()
    ! root.mainloop ()


    Is not it

    BesidesdispPhotoThere seems to be a misspelling in the function.