Tkinter 'module' object is not callable - python

im getting the above error when i run this code snippet. Im trying to error proof user input by creating an error window when the user enters a value not in a dataframe. the code im running is below
import tkinter as tk
import tkinter.messagebox
import pandas as pd
root= tk.TK()
def customer_search():
try:
search = int(entry1.get())
except ValueError:
tk.messagebox("that customer doesnt exist, please enter a new number") #error proofing has to be added tomorrow
search = int(entry1.get())
k = df.loc[df['UniqueID'] == search]
k.to_excel("dashboard.xlsx")
df.to_excel("check.xlsx")
canvas1 = tk.Canvas(root, width=400, height=300)
canvas1.pack()
entry1 = tk.Entry(root)
canvas1.create_window(200, 140, window=entry1)
button1 = tk.Button(text='Enter a customer for analysis', command=customer_search)
button1.pack()
the error i get is as follows
Exception in Tkinter callback
Traceback (most recent call last):
File "C:/Users/....py", line 42, in customer_search
search = int(entry1.get())
ValueError: invalid literal for int() with base 10: 'a'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users...\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users....py", line 44, in customer_search
tk.messagebox("that customer doesnt exist, please enter a new number") #error proofing has to be added tomorrow
TypeError: 'module' object is not callable
Process finished with exit code 0

tk.messagebox is a module containing multiple dialogs, you probably want to use tk.messagebox.showerror("Info Title", "Info content").
Other dialogs are showwarning and showinfo, depending on your use case.

tk.messagebox is a module not a function. A basic difference between modules and functions is that:
You can't call modules, i.e., you can't do module(). (This is precisely the mistake you are making.)
You can call functions, i.e., you can do function(). (This is what you should be doing instead.)
You need to do it this way (in customer_search):
tk.messagebox.showerror("Title here", "that customer doesnt exist, please enter a new number")
where tk.messagebox.showerror is a function in tk.messagebox module.

Related

how to take values from user 2nd time on tkinter entry widget

Here I am writing a code to give a different "mode" option to the user, after pressing the mode button my entry widget pops up and takes two values from the user for further work.
once the user presses the "enter" button my widget will be destroyed.
Here is my code ,it successfully takes values once from the user but when the user gives values 2nd time it shows error.
import tkinter as tk
import time
root=tk.Tk()
root.geometry("600x600")
root.title("User Interface Monitor")
rpm=tk.StringVar()
tim=tk.StringVar()
def enter():
global rpm,tim
root.rpmLabel=tk.Label(root,text="enter rpm value:")
root.rpmLabel.grid(row=0)
root.timeLabel=tk.Label(root,text="enter time in sec")
root.timeLabel.grid(row=1)
root.e1 = tk.Entry(root, textvariable=rpm)
root.e1.grid(row=0, column=1)
root.e1.delete(0,"end")
root.e2 = tk.Entry(root, textvariable=tim)
root.e2.grid(row=1, column=1)
root.e2.delete(0, "end")
#rpm=rpm.get()
#tim=tim.get()
#return rpm,tim
def gett():
global rpm, tim
rpm = rpm.get()
tim = tim.get()
print(rpm)
print(tim)
root.rpmLabel.destroy()
root.e1.destroy()
root.timeLabel.destroy()
root.e2.destroy()
#e1.pack()
#e2.pack()
root.Button1=tk.Button(root,text="MODE1",command=enter)
root.Button1.pack()
root.Button1.place(x=200,y=200)
root.Button2=tk.Button(root,text="Enter",command=gett)#root.Button2.pack()
root.Button2.place(x=260,y=200)
root.mainloop()
Here is my error
C:/Users/RAM/PycharmProjects/timing/rpm.py
23
2
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\RAM\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:/Users/RAM/PycharmProjects/timing/rpm.py", line 25, in gett
rpm = rpm.get()
AttributeError: 'str' object has no attribute 'get'
Process finished with exit code 0
I am new to Python, I couldn't find the solution for this error as tried with "delete" and "reset".
The problem is that rpm starts out as a StringVar and then you reset it to be a string in this line of code:
rpm = rpm.get()
Once that line of code runs, rpm is no longer a StringVar. A simple solution is to use a different name when fetching the value:
rpm_value = rpm.get()
Here, after carefully observing the error messages...
I changed my code line as Bryan suggested
rpm = rpm.get()
tim = tim.get()
to
rpm_value = rpm.get()
tim_value = tim.get()
then it works exactly as I want.
here is my output:
C:/Users/RAM/PycharmProjects/timing/rpm.py
rpm is : 740
time is : 12
want to test again?
enter values again
rpm is : 920
time is : 18
want to test again?
enter values again
Process finished with exit code 0

Errors when trying to clear Tkinter Entry Widget

I am working on a project that will eventually simulate a filter for Twitter posts. I am trying to make a page in Tkinter that will allow the user to enter a Twitter account, and press a button that will add the string to a list and clear the entry field (have yet to code the append function). Code is as follows:
def Add():
F.title('Twitter Filter: Add to Filter')
def h_delete():
Entry.delete(h,first=0,last=END) # should clear entry, instead returns NoneType error
for widget in F.winfo_children():
widget.destroy() # clears widgets of previous window
global a1
a1=tk.StringVar() # declares a variable that will be used to append a list with the text in the Entry
h=tk.Entry(F,textvariable=a1).grid(row=1,column=1) # creates the entry I want cleared
EntryButton=tk.Button(F,text='Add this account',command=h_delete).grid(row=2,column=1) # initiates the entry clearing function
BackButton=tk.Button(F,text='Back to Home',command=Home).grid(row=3,column=1) # returns to home screen
However, when I run the code, I receive a NoneType error, as follows:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1550, in __call__
return self.func(*args)
File "/Users/skor8427/Desktop/Twitter Filter/TwitterFilter.py", line 22, in h_delete
Entry.delete(h,first=0,last=END) # should clear entry, instead returns NoneType error
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 2519, in delete
self.tk.call(self._w, 'delete', first, last)
AttributeError: 'NoneType' object has no attribute 'tk'
I have read various help sections and nothing is working. Anyone have a solution?
h = tk.Entry(F, textvariable=a1)
h.grid(row=1, column=1)
You have to grid h in other line else it will become NoneType
Try this snippet of code instead of
h = tk.Entry(F, textvariable=a1).grid(row=1, column=1)

'bool' object has no attribute 'idKey' using while

I'm writing a graphic application that gives a word after I press a key in my electric piano using a database.
I'm using PyGame, Tkinter and Sqlite.
The application is pretty simple and is almost finished,
but I'm stuck with that error between my piano.py and the frontEnd.py.
The thing is that I want a Label that writes what was the last key I pressed and put it on a canvas.
I know the problem is related to the 'while True' and already changed it with 'while idKey < 176' but with this change I receive the "noneType" error.
This is the current code in my file piano.py
piano.py
import pygame
import pygame.midi
from pygame.locals import *
class backPiano():
def funcPiano(self):
self = backPiano
pygame.init()
pygame.fastevent.init()
event_get = pygame.fastevent.get
event_post = pygame.fastevent.post
pygame.midi.init()
input_id = pygame.midi.get_default_input_id()
i = pygame.midi.Input( input_id )
while True:
events = event_get()
if i.poll():
midi_events = i.read(10)
idKey = midi_events[0][0][0]
if idKey == 176:
return False
And the code in my frontEnd (only the function with the problem):
frontEnd.py
from tkinter import *
from tkinter import ttk, font
import multiprocessing
import time
import os
from database import dictionary, path
from piano import backPiano
class frontEnd(Frame):
def __init__(self, parent):
self.backPiano = backPiano()
def capturePiano(self):
backPiano.funcPiano(self)
superPiano = StringVar()
superPiano.set(backPiano.funcPiano(self).idKey)
labelPiano.configure(textvariable=superPiano)
self.parent.update()
canvasWidth = 500
canvasHeight = 500
w = Canvas(parent, width=canvasWidth, height=canvasHeight)
w.place(x=monitorWidth/2,y=monitorHeight/2, anchor=CENTER)
w.create_image(canvasWidth/2, canvasHeight/2, image=img, anchor=CENTER)
labelPiano = Label(parent)
labelPiano.place(x=monitorWidth/2,y=monitorHeight/2)
In the line 'superPiano.set(backPiano.funcPiano(self).idKey)' I tried:
"superPiano.set(backPiano.idKey)"
But because the variable is inside a function it can't be called with that.
The exact error I have is this:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\admin\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\admin\Desktop\python\frontEnd.py", line 202, in <lambda>
command=lambda : capturePiano(self)).place(x=monitorWidth/9,y=monitorHeight/2,anchor=CENTER)
File "C:\Users\admin\Desktop\python\frontEnd.py", line 187, in capturePiano
superPiano.set(backPiano.funcPiano(self).idKey)
AttributeError: 'bool' object has no attribute 'idKey'
I can't upload all the code, but the error is in the While True but removing it destroys all my code because I need the loop.
Thank you very much (and sorry if I made grammar mistakes).
As the error message says: funcPiano is returning a boolean (True) so when you try to take the idKey it fails, because booleans don't have that.

Tkinter/Python - Simple Login Application NoneType Error [duplicate]

This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 5 years ago.
I am trying to develop a simple login desktop app with tkinter lib. My code is working but when I'm trying to login with correct username and password it's giving the NoneType error. I am writing my codes on Pycharm. Here is my code:
from tkinter import *
window=Tk()
rootname = "Casca"
rootpasswd = "12345"
def loginfunc():
passwd=plogin.get()
name=ulogin.get()
if name==rootname and passwd==rootpasswd:
print("Successfull Login")
else:
print("Unauthorized User")
username=Label(text="Username:",font="Consolas,20").grid(row=0,column=0)
ulogin=Entry(font="Consolas,20",width=8).grid(row=0,column=1)
passwd=Label(text="Password:",font="Consolas,20").grid(row=1,column=0)
plogin=Entry(font="Consolas,20",width=8,show="*").grid(row=1,column=1)
sremember=Checkbutton(text="I forgot my password",font="Consolas,20").grid(row=2,column=0,columnspan=2)
login=Button(text="Login",font="Consolas,20",command=loginfunc).grid(row=3,column=0)
window=mainloop()
And here is the error:
Exception in Tkinter callback
Traceback (most recent call last):
File "BLABLABLA", line 1699, in __call__
return self.func(*args)
File "BLABLABLA", line 9, in loginfunc
passwd=plogin.get()
AttributeError: 'NoneType' object has no attribute 'get'
Entry() returns the instance of the tkinter entry widget
Entry().grid() returns NoneType.
Change your code as shown below
from tkinter import *
window=Tk()
rootname = "Casca"
rootpasswd = "12345"
def loginfunc():
passwd=plogin.get()
name=ulogin.get()
if name==rootname and passwd==rootpasswd:
print("Successfull Login")
else:
print("Unauthorized User")
username=Label(text="Username:",font="Consolas,20").grid(row=0,column=0)
ulogin=Entry(font="Consolas,20",width=8)
ulogin.grid(row=0,column=1)
passwd=Label(text="Password:",font="Consolas,20").grid(row=1,column=0)
plogin=Entry(font="Consolas,20",width=8,show="*")
plogin.grid(row=1,column=1)
sremember=Checkbutton(text="I forgot my password",font="Consolas,20").grid(row=2,column=0,columnspan=2)
login=Button(text="Login",font="Consolas,20",command=loginfunc).grid(row=3,column=0)
window=mainloop()
You will have to do the same thing with the checkbox to get it's value.
You should also consider IntVar and StringVar variables to store the contents of these widgets.
When you do plogin = Entry(...).grid(...) you replace plogin with the result of .grid(), which is None. To fix this, you could do:
plogin = Entry(...)
plogin.grid(...)
and similar for all the other widgets.

How can the variable be used by other functions in TkfileDialog?

I intended to write a GUI to import URLs data then process these data,
so I had 2 buttons. Below is my code.
from Tkinter import *
root=Tk()
root.title('Videos Episodes')
root.geometry('500x300')
def OpenFile(): # import URLs data from local machine
paths=tkFileDialog.askopenfilename()
return paths
def read_files(paths): #read data from the directory from OpenFile
with open(paths) as myfile:
return data
Button(root,text='Input',command=OpenFile).pack()
Button(root,text='Process',command=read_files).pack()
root.mainloop()
My problem is that when 'Process' button clicked, error happened:
Exception in Tkinter callback Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1532, in __call__
return self.func(*args) TypeError: read_files() takes exactly 1 argument (0 given)
How can I fix the bug?
If you want to pass an argument (you didn't specify what), use a lambda:
Button(root,text='Process',command=lambda: read_files('whatever')).pack()
Perhaps, this is what you wanted to do (?):
Button(root,text='Process',command=lambda: read_files(OpenFile())).pack()
or alternatively, you meant to store the result of OpenFile (from clicking the other button) in a global variable, and pass that as argument of read_files...?

Categories

Resources