简体   繁体   中英

Tkinter 'module' object is not callable

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, ie, you can't do module() . ( This is precisely the mistake you are making. )
  • You can call functions, ie, 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM