Python GUI - 2.7 to 3.5 - python

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

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

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

from tkinter import *
App = Tk()
App.geometry("400x400")
L = Label(App, text="Hello")
L.pack()
You don't need use a frame.

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!

Related

I started experimenting with Tkinter on python and for some reason I can't seem to give an attribute to a module which seems to result in an error

import tkinter as tk
def del_text():
textbox.delete("1.0")
window = tk.Tk()
window.configure(bg='black')
greeting = tk.Label(
text="python is a pain",
foreground="white",
background="black"
)
greeting.pack()
root = tk.tkinter()
frame = tk.Frame(root, width=300, height=300)
textbox = tk.Text(frame)
textbox.insert(window,"test")
textbox.after(10, del_text)
window.mainloop()
If I phrased it a bit weird just comment. Also yes i've copied from another guy on stack overflow, it's my first day and I'm messing around. To recreate, just copy and paste this into VScode and look at the error
Change root = tk.tkinter() to root = tk.Tk() It is unclear what you want to achieve, but do note that you are creating two windows: window and root.
Also change textbox.insert(window,"test") to textbox.insert(tk.INSERT,"test")
This should now work, creating the window window and displaying the message 'python is a pain' and creating the window root and displaying the message 'test'.
Hope I was of help!

Using Tkinter in Python Label does not recognize anchor=CENTER

I am new to tkinter and have been using:
from tkinter import *
but have read this is bad practice.
I rewrote a very small bit of code to start using the following:
import tkinter as tk
However when I run the rest of the code. I get the error:
label.place(relx=0.4, rely=0.35, anchor=CENTER)
NameError: name 'CENTER' is not defined
root = tk.Tk()
label = tk.Label(root, text="I am a label widget")
label.place(relx=0.4, rely=0.35, anchor=CENTER)
button = tk.Button(root, text="I am a button")
label.pack()
button.pack()
root.mainloop()
Is this a namespace issue? How can I solve the problem?
* gets all the sub packages. Using import tkinter as tk just changes the name of the package from tkinter to tk.
You have not told your script CENTER is part of tkinter. (you did this automatically when you used *) but now you must do by explicitly telling CENTER is part of tkinter:
tk.CENTER
CENTER is a variable(actually they usually are referred to as constants) of tkinter module which equals to 'center'. So simply replace the line with:
label.place(..., anchor='center')

Do you put Frames in the mainloop() in tkinter?

I am just learning Tkinter and I am wondering if you put Frames() in the mainloop().
For example:
import Tkinter
root = Tk()
f = Frame(root)
root.mainloop()
f.mainloop()
Is this correct?
You should call mainloop exactly once for the life of your program. This is typically done via the root window.

GUI not running properly

I am using mac OS X python. I'm working with GUI right now and is making a simple window with three buttons. I'm trying to configure some buttons to make them do something but it is not working. Can anyone tell me what the problem is? So far I have a little window with three buttons. I wrote the code:
win=Tk()
f=Frame(win)
b1=Button(f,text="one")
b2=Button(f,text"two")
f.pack()
def but1() : print "Button one was pushed"
b1.configure(command=but1)
I am getting the error message invalid syntax for that.
Your program needs to call root.mainloop() on the last line. You also have the problem that you aren't calling pack or grid on the buttons. After adding the call to mainloop() you'll just see any empty window until you call pack or grid on the buttons.
The only thing that I see wrong with your code is that you forgot to include = when you define b2. Running exactly what you have written will raise a Syntax Error.
from Tkinter import *
win = Tk()
f = Frame(win)
b1 = Button(f, text="one")
b2 = Button(f, text="two") # Don't forget the equals sign.
f.pack()
def but1():
print "Button one was pushed"
b1.configure(command=but1)

Python Tkinter module not showing output

I am trying to learn Python and trying something GUI in Python and came across this Tkinter module. My code runs but the window does not appear when I run. My code is as follows:
from Tkinter import *
#to create a root window
root = Tk()
The program runs, gives no errors but the window does not show up.
Add this to your code root.mainloop(), Here's a tutorial.
In response to your comment
#Also note that `from <module> import *` is generally frowned upon
#since it can lead to namespace collisions. It's much better to only
#explicitly import the things you need.
from Tkinter import Tk, Label
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
As other answers have pointed out, you need to call mainloop on the root object.
I recommend an OO-style of programming, and I also recommend not doing a global import (ie: not 'from Tkinter import *').
Here's a template I usually start out with:
import Tkinter as tk
class ExampleView(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
l = tk.Label(self, text="your widgets go here...", anchor="c")
l.pack(side="top", fill="both", expand=True)
if __name__=='__main__':
root = tk.Tk()
view = ExampleView(root)
view.pack(side="top", fill="both", expand=True)
root.mainloop()
This makes it easy to keep your main logic at the start of the file, and keep the creation of the root and the calling of mainloop together, which I think makes the code a little bit easier to understand. It also makes reusing this code a little easier (ie: you could create a larger program where this is one of several windows that can be created)
Add root.mainloop() at the end.

Categories

Resources