import Tkinter
class Application(Frame):
def __init__(self, master):
Frame.__init__(self,master)
self.grid()
self.CreateWidgets()
def CreateWidgets(self):
self.LoginButton = Button(Self)
self.LoginButton["text"] = "Login"
self.LoginButton.grid()
self.QUIT_Button = Button(self)
self.QUIT_Button["text"] = "Quit"
self.QUIT_Button["command"] = self.quit
self.QUIT_Button["fg"] = "red"
root = Tk()
root.title("Login")
root.geometry("500x500")
app = Application(root)
root.mainloop()
This is the youtube tutorial that I have been following: https://www.youtube.com/watch?v=YCLTv6wh3jE&index=39&list=PLB0701884E5AE1B45
And this is the error that keeps occurring:
Traceback (most recent call last):
File "C:\Users\omer\Desktop\test.py", line 3, in <module>
class Application(Frame):
NameError: name 'Frame' is not defined
I am a complete noob at Python and am still learning so any help would be appreciated.
Frame Tk, and Button are all located in the Tkinter namespace. Thus, you have to qualify them to let Python know where they are1:
import Tkinter
class Application(Tkinter.Frame):
...
Tkinter.Frame.__init__(self, master)
...
self.LoginButton = Tkinter.Button(self)
...
self.QUIT_Button = Tkinter.Button(self)
...
root = Tkinter.Tk()
That, or you could just import the names directly:
from Tkinter import Frame, Tk, Button
1If you decide to use this first solution, it would probably be best to import Tkinter like this:
import Tkinter as tk
That way, the code becomes this:
import Tkinter as tk
class Application(Tkinter.Frame):
...
tk.Frame.__init__(self, master)
...
self.LoginButton = tk.Button(self)
...
self.QUIT_Button = tk.Button(self)
...
root = tk.Tk()
which is a lot more brief.
You need to import Frame, Button, Tk.
You can either explicitly import all of them from Tkinter:
from Tkinter import Frame, Button, Tk
or import everything from Tkinter (which is not a good thing to do):
from Tkinter import *
or leave your import as is (import Tkinter) and get Frame, Button and Tk from the Tkinter namespace, e.g. for Frame:
class Application(Tkinter.Frame):
Even better would be to import tkinter in a universal way that would work for both python2 and python3:
try:
# Python2
import Tkinter as tk
except ImportError:
# Python3
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self,master)
self.grid()
self.CreateWidgets()
def CreateWidgets(self):
self.LoginButton = tk.Button(self)
self.LoginButton["text"] = "Login"
self.LoginButton.grid()
self.QUIT_Button = tk.Button(self)
self.QUIT_Button["text"] = "Quit"
self.QUIT_Button["command"] = self.quit
self.QUIT_Button["fg"] = "red"
root = tk.Tk()
root.title("Login")
root.geometry("500x500")
app = Application(root)
root.mainloop()
Also, you have a typo, replace (watch Self):
self.LoginButton = Button(Self)
with:
self.LoginButton = Button(self)
You have to import Frame in order to use it like you are. As it stands you have imported Tkinter, but that doesn't give you access to Frame, Button or Tk the way you've used them. but you either need to do:
from Tkinter import Frame
or
from Tkinter import * (* means 'all' in this case, though this isn't necessary when only using a few modules)
or you could leave your import statement as is(import Tkinter) and change your code like so:
class Application(Tkinter.Frame):
and
self.LoginButton = Tkinter.Button(Self)
However, I would recommend that if you do this, you do:
import Tkinter as tk
That way, you can do tk.Frame and tk.Button etc.
For any modules that you want to use from Tkinter you need to import them also in the same fashion.
You can do single line imports like so:
from Tkinter import Tk, Frame, Button etc.
Check out this info on importing in Python: http://effbot.org/zone/import-confusion.htm
Well it is a bit late but for someone having same error, be sure there is no tkinter.py file in the folder.
I had this same error too. The issue with mine is I had a file called tkinter.py and that was overriding the built-in file tkinter. So to fix it, I changed my filename to something else.
Related
I just need help in why this code does not work. I am unfamiliar with tkinter and this is the code that I was given and was told it should work. I see it says root is not defined but not sure what it should be defined as because I am assuming root is from tkinter? The only thing is that line 2 is grayed out. If you could help it would be greatly appreciated!
import tkinter as tk
from tkinter import *
class MyFirstGUI:
def __init__(self):
root.gui = tk.TK()
root.gui.title("A simple GUI")
root.gui.label = tk.Label(text="This is our first GUI!")
root.label.pack()
root.greet_button = tk.Button(text="Greet", command=self.greet)
root.greet_button.pack()
root.close_button = tk.Button(text="Close", command=self.gui.destroy)
root.close_button.pack()
def greet(root):
print("Greetings!")
root.mainloop()
my_gui = MyFirstGUI()
The identifier root is often assigned from tk.Tk(). If that's the case, then root will be the Tk application and the root of the Tk display graph.
You do all of your setup in the MyFirstGUI class's constructor, but you only use a local variable to keep track of the results. You therefore lose access to all of it when you exit the constructor.
I expect that you want to create everything as a root attribute on self so that your setup is encapsulated and preserved by the class.
Here's how I would rework your code to get it to do what I think you intend:
import tkinter as tk
from tkinter import *
class MyFirstGUI:
def __init__(self):
self.root = Tk()
self.root.title("A simple GUI")
self.root.label = tk.Label(text="This is our first GUI!")
self.root.label.pack()
self.root.greet_button = tk.Button(text="Greet", command=self.greet)
self.root.greet_button.pack()
self.root.close_button = tk.Button(text="Close", command=self.root.destroy)
self.root.close_button.pack()
def greet(root):
print("Greetings!")
my_gui = MyFirstGUI()
my_gui.root.mainloop()
I am trying to create a GUI in Python 3.9.1 with tkinter and am having issues getting a default value to show in an Entry box.
from tkinter import *
from tkinter import ttk
class GUI:
def __init__(self, root, *args):
self.root = root
self.initial_frame = ttk.Frame(self.root)
self.initial_frame.grid(row=0)
ttk.Label(self.initial_frame, text="User question here?").grid(row=1, column=1)
self.ext_len = StringVar(value=4)
ext_len_entry = ttk.Entry(self.initial_frame, textvariable=self.ext_len)
ext_len_entry.grid(row=1, column=2)
root = Tk()
GUI(root)
root.mainloop()
Note: official tkinter docs say to use from tkinter import * and from tkinter import ttk.
See this link and look in the Python section in the step-by-step walkthrough https://tkdocs.com/tutorial/firstexample.html
I've also tried using self.ext_len.set(4) after initializing the entry but before putting it on the grid. I've tried self.ext_len.insert(0,4) as well.
If I add a button with a callback that does nothing, the 4 shows up in the entry box. I found that I don't even have to render it on the grid. Just initializing it is enough. A button without a callback does not work.
Working code:
from tkinter import *
from tkinter import ttk
class GUI:
def __init__(self, root, *args):
self.root = root
self.initial_frame = ttk.Frame(self.root)
self.initial_frame.grid(row=0)
ttk.Label(self.initial_frame, text="User question here?").grid(row=1, column=1)
self.ext_len = StringVar(value=4)
ext_len_entry = ttk.Entry(self.initial_frame, textvariable=self.ext_len)
ext_len_entry.grid(row=1, column=2)
ttk.Button(self.initial_frame, text="Test button", command=self.test_func)
def test_func(self, *args):
pass
root = Tk()
GUI(root)
root.mainloop()
Why does initializing the button cause it to work?
This question concerns Python and Tkinter.
I'd like to use two different ttk themes simultaneously, e.g. 'clam' and 'vista' in one Tkinter window. So I wrote this:
import tkinter as tk
import tkinter.ttk as ttk
class GUI(tk.Tk):
def __init__(self):
super().__init__()
Button_v1()
Button_v2()
class Button_v1(ttk.Button):
def __init__(self, default_text="Test"):
super().__init__(text=default_text)
s = ttk.Style()
s.theme_use('clam')
s.configure(self.winfo_class(), foreground='blue')
self.pack()
class Button_v2(ttk.Button):
def __init__(self, default_text="Test2"):
super().__init__(text=default_text)
s2 = ttk.Style()
s2.theme_use('vista')
s2.configure(self.winfo_class(), foreground='red')
self.pack()
GUI().mainloop()
The code loads, however:
I expected the first Button's foreground to be blue and the second Button's foreground to be red, and for two separate themes to be used.
Instead, both buttons have a red foreground, and both apply the same theme.
Is it possible to use two separate ttk themes in one Tkinter program?
No, it's not possible, because theme has a global meaning in this context. You can represent theme as a set of styles for widgets (for widget layout elements, actually), you can change style and/or create a new one, but you can't use more than one of themes at the time, because widget's layout depends heavily on it.
When you call theme_use('theme_name') tkinter rebuilds all widget's layouts and redraws your entire application, and you can easily check this, thanks to <<ThemeChanged>> virtual event.
Here's a snippet. You will see three prints, caused by each button and by root himself.
try:
import tkinter as tk
import tkinter.ttk as ttk
except ImportError:
import Tkinter as tk
import ttk
class GUI(tk.Tk):
def __init__(self):
super().__init__()
self.bind('<<ThemeChanged>>', lambda event: print('theme changed in root and across all widgets!'))
Button_v1()
Button_v2()
class Button_v1(ttk.Button):
def __init__(self, default_text="Test"):
super().__init__(text=default_text)
s = ttk.Style()
s.theme_use('clam')
s.configure(self.winfo_class(), foreground='blue')
self.pack()
class Button_v2(ttk.Button):
def __init__(self, default_text="Test2"):
super().__init__(text=default_text)
s2 = ttk.Style()
s2.theme_use('vista')
s2.configure(self.winfo_class(), foreground='red')
self.pack()
GUI().mainloop()
If you need different foreground for each button - then create a new style for each of them and use the style parameter:
Here's an another snippet:
try:
import tkinter as tk
import tkinter.ttk as ttk
except ImportError:
import Tkinter as tk
import ttk
class GUI(tk.Tk):
def __init__(self):
super().__init__()
s = ttk.Style()
s.configure('blue.TButton', foreground='blue')
s.configure('red.TButton', foreground='red')
Button_v1()
Button_v2()
class Button_v1(ttk.Button):
def __init__(self, default_text="Test"):
super().__init__(text=default_text, style='blue.TButton')
self.pack()
class Button_v2(ttk.Button):
def __init__(self, default_text="Test2"):
super().__init__(text=default_text, style='red.TButton')
self.pack()
GUI().mainloop()
Everything is well described here and there.
No, it is not possible to use two different themes at the same time.
From the canonical documentation [1] for the function underlying the theme_use method (emphasis added by me):
Without an argument the result is the name of the current theme. Otherwise this command sets the current theme to themeName, and refreshes all widgets.
Here is a simple example showing how you can switch themes on the fly:
# python 2
import Tkinter as tk
import ttk
# python 3
#import tkinter as tk
#import tkinter.ttk as ttk
def switch_theme(theme):
style = ttk.Style()
style.theme_use(theme)
root = tk.Tk()
style = ttk.Style()
for theme in style.theme_names():
button = ttk.Button(root, text=theme,
command=lambda theme=theme: switch_theme(theme))
button.pack(side="top", padx=4, pady=4)
root.mainloop()
No. You can't use two different themes. But, you can create a style:
style = ttk.Style()
style.configure("BW.TLabel", foreground="black", background="white")
l1 = ttk.Label(text="Test", style="BW.TLabel")
l2 = ttk.Label(text="Test", style="BW.TLabel")
You can create another style for a different widget
I am starting to learn Python and the tkinter package and I am writing a program to load a text file on the GUI window. To open the file browser, I installed the button and its necessary function as shown in the below code. The program runs but when I click on the "browse" button, I am getting an attribute error saying : "'assign_1' object has no attribute 'var_filename'". It would be great if anyone could help me with this.
from tkinter import *
from tkinter import messagebox
from tkinter import simpledialog
from tkinter import filedialog
from math import *
from numpy import *
import string
root = Tk()
def close_window_callback(root):
if messagebox.askokcancel("Quit", "Do you really wish to quit?"):
root.destroy()
class assign_1:
def __init__(self,master):
self.master = master
frame = Frame(master)
frame.pack()
self.canvas = Canvas(master,width=1000,height=1000, bg="yellow")
self.button_browse = Button(frame, text="Browse",
command=self.browse_file)
self.button_browse.pack()
self.button_load = Button(frame, text="Load")
self.button_load.pack(side = LEFT)
self.canvas.pack(expand=YES, fill=BOTH)
def browse_file(self):
self.var_filename.set(filedialog.askopenfilename(filetypes=[("allfiles","*"),("pythonfiles","*.txt")]))
filename = self.var_filename.get()
print(filename)
root.protocol("WM_DELETE_WINDOW", lambda root_window=root: close_window_callback(root_window))
assign_1(root)
root.mainloop()
Although, as Rinzler pointed out, your indentation is wrong in the code you posted, that would lead to another error (AttributeError: assign_1 instance has no attribute 'browse_file'). So I'm guessing the indentation in the code you actually use is correct.
The problem is that you try to use self.var_filename.set(...) without having defined what self.var_filename is. If you want it to be a StringVar, which seems to be the case since you use set and get, you have to initialize it. To do this you should put self.var_filename = StringVar(master) in the class' __init__ function. A small example demonstrating this:
root = Tk()
class assign_1:
def __init__(self, master):
self.master = master
self.var_filename = StringVar(master)
self.button_browse = Button(master, text="Browse", command=self.browse_file)
self.button_browse.pack()
def browse_file(self):
self.var_filename.set(filedialog.askopenfilename(filetypes=[("allfiles","*"),("pythonfiles","*.txt")]))
filename = self.var_filename.get()
print(filename)
assign_1(root)
root.mainloop()
However, from the looks of it, in your case there is no need to use a tkinter StringVar, just use a normal string variable:
root = Tk()
class assign_1:
def __init__(self, master):
self.master = master
self.button_browse = Button(master, text="Browse", command=self.browse_file)
self.button_browse.pack()
def browse_file(self):
self.filename = filedialog.askopenfilename(filetypes=[("allfiles","*"),("pythonfiles","*.txt")])
print(self.filename)
assign_1(root)
root.mainloop()
The indentation is wrong. The function browse_file you wanted to define as method of the class assign_1 (use capitalise letters to declare name of classes) is a global function as you defined it.
You have also not defined self.var_filename anywhere, so it will then give you the error:
AttributeError: 'assign_1' object has no attribute 'var_filename'
Under the function close_window_callback, you have also wrong indentation.
I am using Tkinter to build two windows. One the main one, pressing a button leads to the creation of the second window.
This second window does not get focus immediately when it created. That I am able to fix by calling .focus_force(). However, when I call the askdirectory() function from tkFileDialog, the focus changes back to the first window.
How can I prevent that focus switch from happening, without simply calling focus_force() all over the place?
To replicate problem:
from Tkinter import *
from tkFileDialog import *
class app:
def __init__(self, master):
Button(master, command = make_new).grid()
def make_new(self):
root = Tk()
new = new_win(root)
root.mainloop() #here the focus is on the first window
class new_win:
def __init__(self, master):
f = askdirectory() #even after placing focus on second window,
#focus goes back to first window here
I am using Python 2.7.3. Thanks!
the little-documented wm_attributes method might help:
from Tkinter import *
import tkFileDialog
root = Tk()
top = Toplevel()
top.wm_attributes('-topmost', 1)
top.withdraw()
top.protocol('WM_DELETE_WINDOW', top.withdraw)
def do_dialog():
oldFoc = top.focus_get()
print tkFileDialog.askdirectory()
if oldFoc: oldFoc.focus_set()
b0 = Button(top, text='choose dir', command=do_dialog)
b0.pack(padx=100, pady=100)
def popup():
top.deiconify()
b0.focus_set()
b1 = Button(root, text='popup', command=popup)
b1.pack(padx=100, pady=100)
root.mainloop()