【问题标题】:TypeError: 'NoneType' object is not callable in TkinterTypeError:“NoneType”对象在 Tkinter 中不可调用
【发布时间】:2020-05-01 10:10:43
【问题描述】:

您好,我是 tkinter 新手,正在尝试一个简单的 Tkinter GUI,但出现错误:

TypeError: 'NoneType' 对象不可调用

这是我的代码:

from tkinter import *
from tkinter import filedialog
import pandas as pd
import pyodbc
from sqlalchemy import create_engine
import urllib

master = Tk()
master.title("Demo GUI")
master.geometry("900x400+150+150")
master.resizable(0,0)

def browse_file():
    global file_path
    global data_frame
    file_path = filedialog.askopenfilename(title = "Choose the file to upload")
    data_frame = pd.read_excel(file_path)

Label = Label(master,text="Choose the file to upload").grid(row=0)

Button = Button(master,text='Browse',command = browse_file).grid(row=0,column=1,pady=4)

Label_1 = Label(master,text="The file selected: "+file_path).grid(row=1,column=0)

master.mainloop()

我得到的错误是,

TypeError                                 Traceback (most recent call last)
<ipython-input-29-451372edd65a> in <module>
     25 Button = Button(master,text='Browse',command = browse_file).grid(row=0,column=1,pady=4)
     26 
---> 27 Label1 = Label(master,text="The file Choosen: "+file_path).grid(row=1,column=0)
     28 
     29 

TypeError: 'NoneType' object is not callable

【问题讨论】:

标签: python tkinter


【解决方案1】:

'NoneType' 对象不可调用错误是由将对象放置在定义的位置引起的。 所以不是

Label_1 = Label(master,text="The file selected: "+file_path).grid(row=1,column=0)

试试:

Label(master,text="The file selected: "+file_path).grid(row=1,column=0)

或者

Label_1 = Label(master,text="The file selected: "+file_path)
Label_1.grid(row=1,column=0)

也不要使用 Button = Button(master... 而是给变量一个唯一的名称

【讨论】:

    【解决方案2】:

    您所做的错误是一个错字:通过写作:

    Label = Label(master,text="Choose the file to upload").grid(row=0)
    

    您将 grid 调用的结果分配给原始 tk.Label 类型 (Label)。网格回电是None

    因此,当您尝试创建 Label1 时,您实际上是在调用 Label,现在是 None

    只需将行替换为:

     l = Label(master,text="Choose the file to upload")
     l.grid(row=0)
    

    或者干脆

    Label(master,text="Choose the file to upload").grid(row=0)
    

    【讨论】:

    • l = 没有任何意义,因为您所做的只是将None 的值分配给它。
    【解决方案3】:

    这是答案:

    from tkinter import *
    from tkinter import filedialog
    import tkinter as tk
    import pandas as pd
    import pyodbc
    from sqlalchemy import create_engine
    import urllib
    
    master = Tk()
    master.title("Demo GUI")
    master.geometry("900x400+150+150")
    master.resizable(0,0)
    
    def browse_file():
        global file_path
        global data_frame
        file_path = filedialog.askopenfilename(title = "Choose the file to upload")
        data_frame = pd.read_excel(file_path)
    
    Label = Label(master,text="Choose the file to upload").grid(row=0)
    
    Button = Button(master,text='Browse',command = browse_file).grid(row=0,column=1,pady=4)
    
    Label_1 = tk.Label(master,text="The file selected: "+file_path).grid(row=1,column=0)
    
    master.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 2012-04-28
      • 2021-08-18
      • 2016-10-10
      相关资源
      最近更新 更多