Can't find tkinter AttributeError - python

I am writing a code for banking system using python and Tkinter. When I run the program I encounter a few errors. Could someone please help me to figure this out. I have done research about it, but can't find a solution to the problem.
Here is the error:
...File "C:\Users\stjur\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 2256, in _setup
self.tk = master.tk
AttributeError: 'function' object has no attribute 'tk'
And here is my code:
from tkinter import *
root=Tk()
root.title('Avis Banking System')
def win1():
global balance
balance=600
global root
global tries
root=Tk()
root.title('Avis banking system')
entry_1Var=StringVar()
entry_2Var=StringVar()
tries=0
tries=0
def CheckPass():
global tries
if (tries<3):
textvalue=entry_1Var.get()
textvalue=entry_2Var.get()
if textvalue!=('John123') and textvalue!=('Secret'):
tries+=1
label3=Label(root,text='Try again')
label.grid(row=3,column=1,sticky=W,padx=7)
else:
win2()
if (tries==3):
label4=Label(root,text='Used all attempts, entry blocked')
label4.grid(row=3,column=1, sticky=W,padx=7)
entry_1Var=StringVar()
entry_2Var=StringVar()
b1=Button(root,text='Login',width=5,command=CheckPass)
b1.grid(row=1,column=10,sticky='e'+'w',padx=7)
b2=Button(root,text='Reset',width=5,)
b2.grid(row=2,column=10,sticky='e'+'w',padx=7)
l1=Label(root,text='Username:').grid(row=1,padx=7,pady=5,sticky='we')
l2=Label(root,text='Password:').grid(row=2,padx=7,pady=5,sticky='we')
e1=Entry(root,width=30,textvariable=entry_1Var)
e1.grid(row=1,column=1,sticky='e'+'w',columnspan=2)
e2=Entry(root,show='*',width=30,textvariable=entry_2Var)
e2.grid(row=2,column=1,sticky='e'+'w',columnspan=2)
logLabel=Label(root,text='Login',font='bold',fg='Grey')
logLabel.grid(row=0,padx=5,pady=5)
check=Checkbutton(root,text='Keep me logged in')
ckeck.grid(row=4,column=1,sticky=W,pady=5)
if (tries==3):
win2
def win2():
#this is the child window of the main window.
win2=Toplevel(root)
win2.minsize(width=400,height=400)
win2.title("Main Menu")
#Label for the the main menu
lb=Label(win2,text='N&E Scotland Bank\nMain Menu:',bg='#e6e6e6',height=6).pack(fill=X)
btn_1=Button(win2,text='Make Deposit',width=15,height=2,command=win3).pack(pady=5)
btn_2=Button(win2,text='Withdrawal',width=15,height=2).pack(pady=5)
btn_3=Button(win2,text='Accounts',width=15,height=2).pack(pady=5)
btn_4=Button(win2,text='Balance',width=15,height=2).pack(pady=5)
btn_5=Button(win2,text='Exit',width=15,height=2).pack(pady=5)
def win3():#new window for the button make depost
win3=Tk()
win3.title('Make Deposit')
win3.minsize(width=400,height=120)
e1=IntVar()
e2=IntVar()
def MakeDeposit():
num2 = e2.get()
balance = int(num2)
num1 = e1.get()
depositAmount = int(num1)
while depositAmount<=0 or depositAmount%10!=0:
errorLabel = Label(win3,text="That was not a valid amount").grid(row=3,columnspan=5)
return#
textLabel = Label(win3,text=("Your new balance is:\n")).grid(row=3,columnspan=5)
newBalanceLabel = Label(win3,text=(balance+depositAmount)).grid(row=4,columnspan=5, pady=7)
l1=Label(win3,text='Deposit Amount:').grid(row=1,padx=7,pady=5,sticky='we')
l2=Label(win3,text='Balance:').grid(row=2,padx=7,pady=5,sticky='e')
b1=Butto(win3,text='Calculate',width=8,height=3,command=MakeDeposit).grid(row=1,column=10,sticky='e'+'w',padx=7,rowspan=2)
e1=Entry(win3,width=40)
e1.grid(row=1,column=1,sticky='e'+'w',columnspan=3)
e2=Entry(win3,width=40)
e2.grid(row=2,column=1,sticky='e'+'w',columnspan=3)
logLabel=Label(root,text='DEPOSIT',font='bold',fg='Grey').grid(row=0,padx=5,pady=5)
root.mainloop()

I can explain the error message. After fixing the formatting errors in my local copy, I got this:
Traceback (most recent call last):
File "F:\Python\mypy\tem.py", line 84, in <module>
l1=Label(win3,text='Deposit Amount:').grid(row=1,padx=7,pady=5,sticky='we')
File "C:\Programs\Python36\lib\tkinter\__init__.py", line 2760, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Programs\Python36\lib\tkinter\__init__.py", line 2286, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\Programs\Python36\lib\tkinter\__init__.py", line 2256, in _setup
self.tk = master.tk
AttributeError: 'function' object has no attribute 'tk'
The first, parent argument for any Widget creation call must be a widget. In
l1=Label(win3, ...
you passed the win3 function, which does not have a tk attribute.
For the future, please read about MCVEs.

Related

AttributeError: object has no attribute?

Alrighty so this is the error I get:
AttributeError: 'DES' object has no attribute 'summary_output'
So this is what I am trying to do.
When I am on this frame, I am creating a text variable that is then sent to a set class.
class upload_csv(Frame):
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master, width=250, height=160, bg='white')
self.upload_csv_btn = Button(
self.frame,
text="Add Data Source",
fg="DodgerBlue4",
font=("Graph Type", 15),
height=1, width=20,
borderwidth=2,
relief="groove",
command=self.upload)
self.upload_csv_btn.place(x=10, y=10)
self.frame.pack()
def upload(self):
global text
self.xvalues = []
self.yvalues = []
self.xyvalues = []
self.header = []
filename = filedialog.askopenfilename()
if len(filename) != 0:
print('Selected:', filename)
with open(filename) as file:
csvreader = csv.reader(file)
self.header.append(next(csvreader))
for row in csvreader:
if len(row) == 3:
self.xvalues.append(int(row[0]))
self.yvalues.append(int(row[1]))
self.xyvalues.append(int(row[2]))
text = (
self.header[0][0]+ ": " + str(self.xvalues).replace('[','').replace(']','') +
"\n\n" + self.header[0][1] + ": " + str(self.yvalues).replace('[','').replace(']','') +
"\n\n" + self.header[0][2] + ": " + str(self.xyvalues).replace('[','').replace(']',''))
elif len(row) == 2:
self.xvalues.append(row[0])
self.yvalues.append(row[1])
text = (
self.header[0][0] + ": " + str(self.xvalues).replace('[','').replace(']','') +
"\n\n" + self.header[0][1] + ": " + str(self.yvalues).replace('[','').replace(']',''))
# -------------------------------------------------------------------------
s = Set(text)
s.set_summary()
#-----------------------------------------------------------------------
Using the upload class, I am sending the variable by calling the set class, and calling the set_summary method. With this set class, I am setting the string as a an object item, that is then send to my DES class. I want this item to be set on a tk textbox element as a summary. I receive the text fine in the DES class, but I get the following error when trying to modify the summary element.
The error I get:
Traceback (most recent call last):
File "C:\Users\***\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:\Users\***\Documents\Workspace\***\***\view\upload_csv.py", line 115, in upload
s.set_summary()
File "C:\Users\***\Documents\Workspace\***\***\view\Set.py", line 14, in set_summary
s.set_summary_text()
File "C:\Users\***\Documents\Workspace\***\***\view\test.py", line 164, in set_summary_text
print(self.summary_output)
AttributeError: 'DES' object has no attribute 'summary_output'
My set class:
class Set:
def __init__ (self, summary):
self.summary = summary
def set_summary(self):
print(self.summary)
s = DES(self.summary)
s.set_summary_text()
My DES Class:
class DES(Frame):
def __init__(self, summary):
self.summary = summary
def createFrame(self, master):
self.frame = tk.Frame(master, width=750, height=968,bg='white')
self.summary_output = tk.Text(
self.frame,
height=8,
width=78,
bg="gray95",
borderwidth=2,
relief="groove",
font=("Arial", 12))
self.summary_output.configure(state='disabled')
self.summary_output.place(x=20, y=610)
self.frame.pack()
def set_summary_text(self):
print(self.summary)
print(self.summary_output)
self.summary_output.configure(state='normal')
self.summary_output.delete('1.0', END) # Remote all text
self.summary_output.insert('end',self.summary)
self.summary_output.configure(state='disabled') #Make text widget read only
def main():
global root
root = tk.Tk()
# app = DES(root)
# app = DES.createFrame(root)
s = DES("")
s.createFrame(root)
root.mainloop()
if __name__ == '__main__':
main()
Edit:
So after trying the answer I got the following error, all I did was add the suggestion:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\***\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:\Users\***\Documents\Workspace\\***\\***\view\upload_csv.py", line 115, in upload
s.set_summary()
File "C:\Users\\***\Documents\Workspace\\***\view\Set.py", line 22, in set_summary
s.createFrame(root)
File "C:\Users\\***\Documents\Workspace\\***\view\test.py", line 120, in createFrame
self.canvas.draw() # Create the graph canvas
File "C:\Users\\***\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 11, in draw
self._master.update_idletasks()
AttributeError: 'str' object has no attribute 'update_idletasks'
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\\***\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:\Users\\***\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\backends\_backend_tk.py", line 235, in filter_destroy
self._master.update_idletasks()
AttributeError: 'str' object has no attribute 'update_idletasks'
So I remove the matplot graph and got this error:
So maybe the graph is interfering? Im not sure, I need the graph.
The summary_output in DES class, will be defined in the
createFrame method.
You first instatiated from the DES class in the Set.set_summary()
method and then called the set_summary_text() method, which it uses
the summary_output. That's not correct, since the summary_output has not been defined, yet.
You should first, call the createFrame() method to define the
summary_output attribute and then call the set_summary_text() to
use summary_output.
Do something like this, in the Set class:
class Set:
def __init__ (self, summary):
self.summary = summary
def set_summary(self):
global root
print(self.summary)
s = DES(self.summary)
s.createFrame(root)
s.set_summary_text()
Or do whatever you think it's best for you, but you should define the summary_output first, and then print or use it.

Can't get tkinter's StringVar's trace method to work

Whenever I try to run my code, which looks a bit like this:
from tkinter import OptionMenu, StringVar, Tk
class Foo(OptionMenu):
def __init__(self, master, options):
self.bar = StringVar()
self.bar.set(options[0])
self.bar.trace("w", lambda: self.mouseCallback("foobar"))
super().__init__(master, self.bar, *options)
def mouseCallback(self, baz):
print(baz)
def mainCycle():
while True:
root.update()
if __name__ == "__main__":
opts = ["A", "LONG", "LIST", "OF", "OPTIONS"]
root = Tk()
foobarbaz = Foo(root, opts)
foobarbaz.pack()
mainCycle()
I get the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python35\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "C:\Program Files\Python35\lib\tkinter\__init__.py", line 3285, in __call__
self.__var.set(self.__value)
File "C:\Program Files\Python35\lib\tkinter\__init__.py", line 260, in set
return self._tk.globalsetvar(self._name, value)
_tkinter.TclError: can't set "PY_VAR0":
And even after a lot of "Stack Overflowing", I still can't get it to work. How can I avoid/fix this error?
The signature of the callback function for StringVar.trace() should be something like def callback(*args), therefore the lambda you use in StringVar.trace() should be changed to:
self.bar.trace("w", lambda *args: self.mouseCallback("foobar"))

Python Tkinter : AttributeError: Button instance has no __call__ method

I don't know why this is giving me an attribute error. I want my blah() function to shuffle the cards. I'm calling the builtin function shuffle() from random.
Error:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
return self.func(*args)
File "gui1.py", line 105, in blah
shuffle(cards)
AttributeError: Button instance has no __call__ method
Here's the code snippet:
def blah():
global card_count
global path
shuffle(cards)
card_count = 0
path = generate_paths(cards)
print "Cards Shuffled"
shuffle = Button(frame_buttons, text = "SHUFFLE",height = 2, width = 10,command =blah)
shuffle.grid(row = 2 , padx = 40, pady = 40)
shuffle is the name of the function in random. However, it's also the name of the Button. Change the Button's name to something like shuffle_button and you should be fine.

Listbox seen as Nonetype, why?

self.__tracksListBox = Listbox(self.__master, width=100, height=25).grid(row=0, column=1,
rowspan=4)
I'm trying to display options in a Listbox, but the following error is thrown:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
return self.func(*args)
File "C:\Users\aawiesinger5000\Desktop\CS 110 Final Project\layoutGUI.py", line 213, in showTracks
self.__tracksListBox.insert(END, titleStr)
AttributeError: 'NoneType' object has no attribute 'insert'
when I try to use the list box like this:
if tracks:
titleStr = ""
for track in tracks:
titleStr += track + "\n"
self.__tracksListBox.insert(END, titleStr)
Does anyone know why?
Thank you!!
grid method does not return; implicitly return None.
Separate the following statement into two statements:
self.__tracksListBox = Listbox(self.__master, width=100, height=25).grid(row=0, column=1,
rowspan=4)
like:
self.__tracksListBox = Listbox(self.__master, width=100, height=25)
self.__tracksListBox.grid(row=0, column=1, rowspan=4)

Storing frames in list

I'm trying to store frames in a list so i can get my widgets 3 by 3 on top of each other whit a small space in between them
But i can't seem to make it work.
Code:
self.cboCombo = []
self.frame_type = []
for x in range(7):
self.cboCombo.append([])
for y in range(3):
self.cboCombo[x].append([])
for i in range(7):
self.frame_type.append(i)
self.frame_type[i] = Frame(self.frame_type)
self.frame_type[i].pack(side=TOP, expand=1, ipady= 11)
self.cboCombo[i][0] = ttk.Combobox(self.frame_type[i], values=self.valCombo, textvariable=self.varCombo[1][0])
self.cboCombo[i][0].pack(padx= 5)
self.cboCombo[i][1] = ttk.Combobox(self.frame_type[i], values=self.valCombo, textvariable=self.varCombo[1][1])
self.cboCombo[i][1].pack(padx= 5)
self.cboCombo[i][2] = ttk.Combobox(self.frame_type[i], values=self.valCombo, textvariable=self.varCombo[1][2])
self.cboCombo[i][2].pack(padx= 5)
Error message:
File "File.py", line 89, in __init__
self.frame_type[i] = Frame(self.frame_type)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2453, in __init__
Widget.__init__(self, master, 'frame', cnf, {}, extra)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1965, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1943, in _setup
self.tk = master.tk
AttributeError: 'list' object has no attribute 'tk'
Is there a solution to this or another way to get it done?
Thanks
Following the Tkinter docs:
w = Frame ( master, option, ... )
Parameters:
master: This represents the parent window.
Your self.frame_type doesn't look like object represents the parent window.

Categories