TypeError:“NoneType”对象在 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


慕神8447489
浏览 156回答 3
3回答

qq_花开花谢_0

'NoneType' 对象不可调用错误是由将对象放置在定义的位置引起的。所以而不是Label_1&nbsp;=&nbsp;Label(master,text="The&nbsp;file&nbsp;selected:&nbsp;"+file_path).grid(row=1,column=0)尝试:Label(master,text="The&nbsp;file&nbsp;selected:&nbsp;"+file_path).grid(row=1,column=0)或者Label_1&nbsp;=&nbsp;Label(master,text="The&nbsp;file&nbsp;selected:&nbsp;"+file_path) Label_1.grid(row=1,column=0)也不要使用 Button = Button(master... 而是给变量一个唯一的名称

青春有我

这是答案:from tkinter import *from tkinter import filedialogimport tkinter as tkimport pandas as pdimport pyodbcfrom sqlalchemy import create_engineimport urllibmaster = Tk()master.title("Demo GUI")master.geometry("900x400+150+150")master.resizable(0,0)def browse_file():&nbsp; &nbsp; global file_path&nbsp; &nbsp; global data_frame&nbsp; &nbsp; file_path = filedialog.askopenfilename(title = "Choose the file to upload")&nbsp; &nbsp; 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()

30秒到达战场

您所做的错误是一个错字:通过编写:Label&nbsp;=&nbsp;Label(master,text="Choose&nbsp;the&nbsp;file&nbsp;to&nbsp;upload").grid(row=0)您将grid调用结果分配给原始 tk.Label 类型 (&nbsp;Label)。网格回电是None因此,当您尝试创建 Label1 时,您实际上是在调用现在的 LabelNone只需将行替换为:&nbsp;l&nbsp;=&nbsp;Label(master,text="Choose&nbsp;the&nbsp;file&nbsp;to&nbsp;upload") &nbsp;l.grid(row=0)或者干脆Label(master,text="Choose&nbsp;the&nbsp;file&nbsp;to&nbsp;upload").grid(row=0)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python