【问题标题】:Tkinter 'module' object is not callableTkinter“模块”对象不可调用
【发布时间】:2020-04-25 15:59:00
【问题描述】:

我在运行此代码 sn-p 时遇到上述错误。我试图通过在用户输入不在数据框中的值时创建错误窗口来防止用户输入错误。我正在运行的代码如下

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()

我得到的错误如下

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.showinfo()等等。
  • 确定这是你得到的错误吗?在收到其他错误之前,您应该收到错误 module 'tkinter' has no attribute TK
  • jizhihaoSAMA 已修复,谢谢!

标签: python pandas tkinter error-handling


【解决方案1】:

tk.messagebox 是一个包含多个对话框的模块,您可能想使用tk.messagebox.showerror("Info Title", "Info content")

其他对话框为 showwarningshowinfo,具体取决于您的用例。

【讨论】:

    【解决方案2】:

    tk.messagebox 是一个模块而不是一个函数。模块和函数的一个基本区别是:

    • 不能调用模块,即不能调用module()。 (这正是你所犯的错误。
    • 你可以调用函数,即你可以做function()。 (这是你应该做的。

    你需要这样做(customer_search):

    tk.messagebox.showerror("Title here", "that customer doesnt exist, please enter a new number")
    

    其中tk.messagebox.showerrortk.messagebox 模块中的函数

    【讨论】:

      猜你喜欢
      • 2017-06-17
      • 2012-09-28
      • 2014-09-21
      • 2022-01-09
      • 2021-12-26
      • 2011-05-30
      • 2021-11-22
      相关资源
      最近更新 更多