1
from tkinter import *

#Create the window
root = Tk()

#Modify root window
root.title("Simple GUI")
root.geometry("200x50")

app = frame(root)
label = Label(app, text = "This is a label")
label.grid()


#kick of the event loop
root.mainloop()

I am following a tutorial of YouTube to learn about Python tkinter GUI. But when I run the above code it comes with an error.

Traceback (most recent call last):
  File "C:/Users/Nathan/Desktop/Python/Python GUI/Simple GUI.py", line 14, in <module>
    app = frame(root)
NameError: name 'frame' is not defined

I know it is something to do with frame, I tried Frame and it doesn't work. Can you please help me make it work, Thanks!

I am currently using Python 3.5 and the tutorial is in 2.7

Joseph Farah
  • 2,463
  • 2
  • 25
  • 36
N. Wyatt
  • 15
  • 4

4 Answers4

1

You did get the fact that the 2.x module is named Tkinter, but in 3.x it is named tkinter. However, the Frame class did not change the first letter to lower case. It is still Frame.

app = Frame(root)

One way to overcome the import difference is in ImportError when importing Tkinter in Python

Community
  • 1
  • 1
lit
  • 14,456
  • 10
  • 65
  • 119
  • @silentphoenix: the OP is incorrect, or is leaving out some details. The answer is indeed to use `Frame` rather than `frame`. – Bryan Oakley Oct 25 '15 at 16:49
1

There are two things wrong with your script. The first one gives the error, and you have already worked out how to fix that:

app = Frame(root)

The second problem is that the label won't appear inside the frame without proper layout management. To fix that, call pack() on the frame:

label = Label(app, text = "This is a label")
label.grid()
app.pack()
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
0
from tkinter import *
App = Tk()
App.geometry("400x400")
L = Label(App, text="Hello")
L.pack()

You don't need use a frame.

  • While it's true the OP doesn't need to use a frame, that doesn't answer the question that was asked. Also, this code won't work because the label has a parent of `v`, but you've only created a widget named `V` -- case is important. – Bryan Oakley Oct 25 '15 at 16:48
0

First, understand that whenever you want to create a label or frame make sure you use its first letter capital. For ex. Label() or Frame(). In your above example use: app = Frame(root) and then you need to use "grid()" to just nicely pack your frame. In your above example use: app.grid() Best luck!

Yash Tile
  • 51
  • 7