N. Wyatt
N. Wyatt

Reputation: 15

Python GUI - 2.7 to 3.5

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

Upvotes: 1

Views: 735

Answers (4)

Yash Tile
Yash Tile

Reputation: 51

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!

Upvotes: 0

Ronald Hern&#225;ndez
Ronald Hern&#225;ndez

Reputation: 87

from tkinter import *
App = Tk()
App.geometry("400x400")
L = Label(App, text="Hello")
L.pack()

You don't need use a frame.

Upvotes: 0

ekhumoro
ekhumoro

Reputation: 120608

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()

Upvotes: 1

lit
lit

Reputation: 16236

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

Upvotes: 1

Related Questions