简体   繁体   English

Tkinter '模块' object 不可调用

[英]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.当用户输入不在 dataframe 中的值时,我试图通过创建错误 window 来防错用户输入。 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") . tk.messagebox是一个包含多个对话框的模块,您可能想要使用tk.messagebox.showerror("Info Title", "Info content")

Other dialogs are showwarning and showinfo , depending on your use case.其他对话框是showwarningshowinfo ,具体取决于您的用例。

tk.messagebox is a module not a function. tk.messagebox不是 function 的模块。 A basic difference between modules and functions is that:模块和函数之间的基本区别是:

  • You can't call modules, ie, you can't do module() .你不能调用模块,即你不能做module() ( This is precisely the mistake you are making. ) 这正是您所犯的错误。
  • You can call functions, ie, you can do function() .您可以调用函数,即您可以执行function() ( This is what you should be doing instead. ) 这是你应该做的。

You need to do it this way (in customer_search ):您需要这样做(在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 .其中tk.messagebox.showerrortk.messagebox模块中的tk.messagebox

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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