TypeError:“ NoneType”对象在Tkinter中不可调用

时间:2020-05-01 10:10:43

标签: python tkinter

嗨,我是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

3 个答案:

答案 0 :(得分:0)

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

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)

答案 1 :(得分:0)

'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 :(得分:0)

这是答案:

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