Getting Attribute error in Python - python-2.7

Here is my code along with the error message.
from Tkinter import *
class Window01 (Frame):
def __init__(self, master):
Frame.__init__(self)
self.reveal()
self.create_widget()
self.grid()
def create_widget(self):
self.lbl = Label (self, text = "This is a Widget App.")
self.lbl.grid(row =1, column =0, columnspan =2, sticky = W)
self.entbx = Entry(self)
self.entbx.grid(row = 1, column = 1, sticky = W)
self.bttn = Button (self, text = "Widget Button", command = self.reveal)
self.bttn.grid(row = 2, column = 0, sticky = W)
self.txt = Text (self, width =35, height = 5, wrap = WORD)
self.txt.grid(row = 3, column = 0, columnspan =2, sticky = W)
def reveal (self):
contents = self.entbx.get()
if contents =="magic":
message = "Access Granted"
else:
message = "Denied"
self.txt.delete(0.0, END)
elf.txt.insert(0.0, message)
root = Tk()
root.title ("Widget_Button")
root.geometry ("300x150")
app = Window01 (root)
root.mainloop()
File "C:\PyDev\Py_Widgets101\src\Py_Widget03.py", line 10, in init
self.reveal()
File "C:\PyDev\Py_Widgets101\src\Py_Widget03.py", line 30, in reveal
contents = self.entbx.get()
AttributeError: Window01 instance has no attribute 'entbx'

self.entbx is created by create_widget(). You are calling reveal() -- which requires self.entbx -- before you've called create_widget():
self.reveal()
self.create_widget()

Related

Python Tkinter: Update entry field from another class

I have a Python Tkinter GUI class as following:
import Tkinter as tk
class AppClient(tk.Frame):
def __init__(self, master):
error_class = ErrorClass()
self.val_error_code = error_class.error_message
tk.Frame.__init__(self, master)
self.pack()
##Frame for error message display
error_frame = tk.Frame(self, highlightbackground="violet", highlightcolor="violet", highlightthickness=1, width=600, height = 60, bd= 0)
error_frame.pack(padx = 10, pady = 5, anchor = 'w')
## error code: Label and Entry
tk.Label(error_frame, text = 'Error message').grid(row = 0, column = 0, sticky = 'w')
self.error_code = tk.Entry(error_frame, background = 'white', width = 27)
self.error_code.grid(row = 0, column = 1, sticky = 'w', padx = 5, pady = 5)
def update_error_messsage(self):
self.error_code.delete(0, 'end')
self.error_code.insert(0, self.val_error_code)
if __name__ == '__main__':
root = tk.Tk()
app_client = AppClient(root)
app_client.mainloop()
I want to update the entry field error_code dynamically by using the function update_error_message. The problem is, error message is continuously updated from another class ErrorClass() and I am receiving the error message to my AppClient class by variable error_message.
How can I update the error_code entry filed whenever the value of the variable error_message got updated in another class (ErrorClass())
The simple solution is that you need a reference to your instance of AppClient inside of ErrorClass, and then allow the instance of ErrorClass to pass the new value to the app
For example:
class AppClient(tk.Frame):
def __init__(self, master):
error_class = ErrorClass(self)
...
def update_error_messsage(self, error_code):
self.error_code.delete(0, 'end')
self.error_code.insert(0, error_code)
class ErrorClass():
def __init__(self, app):
self.app = app
def update_error(self):
...
error_code = 42
self.app.update_error_message(error_code)

can't change the list

import tkinter as tk ## most of this is for the Bottoni class
from tkinter import *
import os
import string
import PIL.Image
import PIL.ImageTk
import tkinter.ttk
elenco1 = [elemento1, elemento2, elemento3, elemento4]
elenco2 = [elemento5, elemento6, elemento7, elemento8]
elenco3 = [elemento9, elemento10, elemento11, elemento12]
elenco4 = [elemento13, elemento14, elemento15, elemento16]
BOT = [] ## this is for the Bottoni class
IM = [] ## and this too
elenco = elenco1
class ChangeElenco:
def __init__ (self):
def changeElenco(regione):
if regione == "N":
self.elenco = elenco1
elif regione == "K":
self.elenco = elenco2
elif regione == "J":
self.elenco = elenco3
elif regione == "H":
self.elenco = elenco4
elenco = self.elenco
self.radio1 = Radiobutton(text = '1', value = 1, command = lambda : changeElenco("N")).place (x = 920, y = 150)
self.radio2 = Radiobutton(text = '2', value = 2, command = lambda : changeElenco("K")).place (x = 920, y = 170)
self.radio3 = Radiobutton(text = '3', value = 3, command = lambda : changeElenco("J")).place (x = 920, y = 190)
self.radio4 = Radiobutton(text = '4', value = 4, command = lambda : changeElenco("H")).place (x = 920, y = 210)
class secondWindow:
def __init__(self):
self.secondWindow = Tk()
self.secondWindow.geometry ('500x650+400+30')
class Bottoni:
def __init__ (stringa, elenco):
...
def Destroy (self):
...
class mainWindow:
def __init__(self):
self.mainWindow = Tk()
self.mainWindow.geometry ('1130x650+100+10')
self.mainWindow.title('mainWindow')
def mainEnter (self):
testoEntry = StringVar()
testoEntry.set('')
self.mainEntry = Entry(self.mainWindow, textvariable = testoEntry).place (x = 920, y = 20)
def search ():
testoEntry2 = testoEntry.get()
if testoEntry2 == "":
pass
else:
testoEntry2 = testoEntry2.lower()
Bottoni.Destroy(self)
BOT = []
ChangeElenco() ################ here i need the program to change my list "elenco" when i press a radio button
Bottoni(testoEntry2, elenco)
def tracing(a, b, c):
if len(testoEntry.get()) > 2:
search()
testoEntry.trace('w', tracing)
self.button4Entry = Button (self.mainWindow, text = 'search', command = search).place (x = 1050, y = 17)
MW = mainWindow()
ChangeElenco()
MW.mainEnter()
mainloop()
The problem is that in this code, I need the ChangeElenco class to change me the list, and the list I need is the one I choose with the radio button, but in fact it changes just inside the changeElenco function inside that class, and then the list remains the one I had at the beginning...

Object Oriented Tkinter Coding: Changing text and Updating

So I have been teaching myself Object Oriented Programming for Tkinter projects as I clearly see that they are much more organized for large amounts of coding. However I must admit that I've been coasting by simply copying various bits of coding from online, not fully understanding what its purpose is.
This has lead me to the point that my code does not work at all and I have no idea why not. The first issue is an issue with simply changing an aspect of other widgets.
I have this sample code:
import Tkinter as tk
LARGE_FONT = ("Verdana", 12)
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill = "both", expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0 , weight = 1)
self.frames = {}
frame = StartPage(container, self)
self.frames[StartPage] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text = "Start Page", font = LARGE_FONT)
label.pack(pady = 10, padx = 10)
button = tk.Button(self, text = "Change Label", command = self.change)
button.pack(pady = 10, padx = 10)
def change(self):
label["text"] = "It has changed"
app = SeaofBTCapp()
app.mainloop()
Which SHOULD be a simple enough code that, with a button press, change the label from "Start Page" to "It has changed". But whenever I run it, it says that the global variable "label" is not defined. Additionally, if I then change it to self.label, it states that StartPage instance has no attribute 'label'. I don't know what I'm doing wrong.
Additionally, in a similar vein, I'm working on a project that has a SideBar class and a Main class tied to one MainApplication class. The Main class takes a value and displays it on a Frame in the Main class. Following this, a button in the SideBar increases that value by 1. But the Main display doesn't update and I have no idea how to tie the Main updating with the button in the SideBar.
import Tkinter as tk
something = [0, 6]
class Main():
def __init__(self, root):
mainboard = tk.Frame(root, height = 100, width = 100)
self.maincanvas = tk.Canvas(mainboard, bd = 1, bg = "white")
mainboard.grid(row = 0, column = 0)
self.maincanvas.grid(row = 0, column = 0)
self.maincanvas.create_text(45, 50, anchor = "center", text = str(something[1]))
class SideBar():
def __init__(self, root):
sidebarframe = tk.Frame(root, height = 100, width = 100)
button = tk.Button(sidebarframe, width = 20, text = "Change Value", command = self.add)
sidebarframe.grid(row = 0, column = 1)
button.grid(row = 0, column = 0)
def add(self):
something[1] += 1
print something[1]
class MainApplication():
def __init__(self, parent):
self.parent = parent
self.sidebar = SideBar(self.parent)
self.main = Main(self.parent)
if __name__ == "__main__":
root = tk.Tk()
MainApplication(root)
root.mainloop()
All help would be appreciated, but please try and not use a lot of technical terms, as I am still learning.
In the first scenario replace:
label = tk.Label(self, text = "Start Page", font = LARGE_FONT)
label.pack(pady = 10, padx = 10)
With:
self.label = tk.Label(self, text = "Start Page", font = LARGE_FONT)
self.label.pack(pady = 10, padx = 10)
And also in the function change put it like this:
self.label["text"] = "It has changed"
And in your second problem i changed the code a little bit so it works:
import Tkinter as tk
something = [0, 6]
class Main():
def __init__(self, root):
mainboard = tk.Frame(root, height = 100, width = 100)
self.maincanvas = tk.Canvas(mainboard, bd = 1, bg = "white")
mainboard.grid(row = 0, column = 0)
self.maincanvas.grid(row = 0, column = 0)
self.maincanvas.create_text(45, 50, anchor = "center", text = str(something[1]))
class SideBar():
def __init__(self, root, main):
self.main = main # Putting the main object to self.main
sidebarframe = tk.Frame(root, height = 100, width = 100)
button = tk.Button(sidebarframe, width = 20, text = "Change Value", command = self.add)
sidebarframe.grid(row = 0, column = 1)
button.grid(row = 0, column = 0)
def add(self):
something[1] += 1
self.main.maincanvas.delete("all") # Removing everything from canvas
self.main.maincanvas.create_text(45, 50, anchor = "center", text = str(something[1])) # Inserting the new value
print something[1]
class MainApplication():
def __init__(self, parent):
self.parent = parent
self.main = Main(self.parent) # The main needs to run first
self.sidebar = SideBar(self.parent, self.main) # So that SideBar can use its canvas
if __name__ == "__main__":
root = tk.Tk()
MainApplication(root)
root.mainloop()

Python 2.7: Tkinter: Classes - Attribute Error [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 7 years ago.
Hello I am making a GUI for a program with Tkinter, but I keep getting an error:
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
File "C:\Users\jorge\Desktop\new 1.py", line 53, in browse_button
self.browse_entry.delete(0,END)
AttributeError: 'NoneType' object has no attribute 'delete'"
I am a noobie what it comes to classes so I don't know what I'm making wrong.
If you could help me I would appreciate it.
Thanks in advance!!
from Tkinter import *
from tkFileDialog import *
import ttk,os
class App(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
self.title("Subtitles Master")
self.iconbitmap(default = r"C:\Users\jorge\Desktop\Programas Python\notepad.ico")
mainframe = Frame(self)
mainframe.pack(side = "top")
mainframe.grid_rowconfigure(0, weight = 1)
mainframe.grid_columnconfigure(0, weight = 1)
self.frames = {}
for F in (StartPage, Timing):
frame = F(mainframe, self)
self.frames[F] = frame
frame.grid (row = 0, column = 0, sticky = "nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
filler1 = Frame(self, width = 30, height = 20).grid(column = 4, row = 0, rowspan = 2)
syncsubs_button = ttk.Button(self, text = "Sync Subtitles", state = DISABLED).grid(column = 5, row = 0)
timing_button = ttk.Button(self, text = "Edit Timing", command = lambda: controller.show_frame(Timing)).grid(column = 5, row = 1)
filler2 = Frame(self, width = 20, height = 20).grid(column = 6, row = 0, rowspan = 2)
syncsubs_label= Label(self, font = ("Alien Encounters", 20), text = "Sync Subtitles", fg = "dark green").grid(row = 2, columnspan = 6)
browse_label = Label(self, font = ("Calibri",12), text = "Subtitles to sync:").grid(row = 3, column = 0, sticky = "w")
filename1 = StringVar()
self.browse_entry = Entry(self, textvariable = filename1, width = 50).grid(row = 4, column = 1, columnspan = 5)
browse_button = ttk.Button(self, text = "Browse", command = self.browse_button).grid(row = 4, column = 0)
def browse_button(self):
filename = askopenfilename(title = "Choose a file",filetypes = (("Subtitles", "*.srt"), ("Text", "*.txt")))
self.browse_entry.delete(0,END)
self.browse_entry.insert(0,filename)
return filename
class Timing(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
filler1 = Frame(self, width = 30, height = 20).grid(column = 4, row = 0, rowspan = 2)
syncsubs_button = ttk.Button(self, text = "Sync Subtitles", command = lambda: controller.show_frame(StartPage)).grid(column = 5, row = 0)
timing_button = ttk.Button(self, text = "Edit Timing", state = DISABLED).grid(column = 5, row = 1)
filler2 = Frame(self, width = 20, height = 20).grid(column = 6, row = 0, rowspan = 2)
syncsubs_label= Label(self, font = ("Alien Encounters", 20), text = "Edit Timing", fg = "dark red").grid(row = 2, columnspan = 6)
window = App()
window.mainloop()
The problem is this line:
# returns None (Entry.grid(...))
self.browse_entry = Entry(self, textvariable = filename1, width = 50).grid(row = 4, column = 1, columnspan = 5)
Change to two lines:
# returns entry
self.browse_entry = Entry(self, textvariable = filename1, width = 50)
# do stuff with entry
self.browse_entry.grid(row = 4, column = 1, columnspan = 5)

Python-2.7.10 Password GUI not working

Working on a Python 2.7.10 program, for a password gui but it is giving the error that Application is not defined. I am not sure if there is something wrong with Class Application(Frame) or app = Application(root), because it ask for me to define Application but I don't why they would ask me.
from Tkinter import *
class Application(Frame):
def _init_(self, master):
Frame._init_(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.instruction = Label(self, text = "Enter the password")
self.instruction.grid(row = 0, column = 0, columnspan = 2, sticky = W)
self.password = Entry(self)
self.password.grid(row = 1, column = 1, sticky = W)
self.submit_button = Button(self, text = "Submit", commmand = self.reveal)
self.submit_button.grid(row = 2, column = 0, sticky = W)
self.text = Text(self, width = 35, height = 5, wrap = WORD)
self.text.grid(row = 3, column = 0, columnspan = 2, sticky = W)
def reveal(self):
content = self.password.get()
if content == "password":
message = "You have the order"
else:
message = "Access denied."
self.text.delete(0.0, END)
self.text.insert(0.0, message)
root = Tk()
root.title("Password")
root.geometry("250x150")
app= Application(root)
root.mainloop()
I've fixed your code, here it is:
from Tkinter import *
class Application(Frame):
def __init__(self, master):
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.instruction = Label(self, text = "Enter the password")
self.instruction.grid(row = 0, column = 0, columnspan = 2, sticky = W)
self.password = Entry(self)
self.password.grid(row = 1, column = 1, sticky = W)
self.submit_button = Button(self, text = "Submit", command = self.reveal)
self.submit_button.grid(row = 2, column = 0, sticky = W)
self.text = Text(self, width = 35, height = 5, wrap = WORD)
self.text.grid(row = 3, column = 0, columnspan = 2, sticky = W)
def reveal(self):
content = self.password.get()
if content == "password":
message = "You have the order"
else:
message = "Access denied."
self.text.delete(0.0, END)
self.text.insert(0.0, message)
root = Tk()
root.title("Password")
root.geometry("250x150")
app= Application(root)
root.mainloop()
Your problems were:
You used _init_ instead of __init__
Typo of "commmand" instead of "command"
Last two lines of procedure reveal in Application were incorrectly indented (logic error, not syntax or run-time)
It wasn't really clear what your question was to be honest and I just did this in Python 3.4.3 and made changes so that it should run in Python 2.7.10. I hope that helps.