亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

TypeError:“NoneType”對象在 Tkinter 中不可調用

TypeError:“NoneType”對象在 Tkinter 中不可調用

慕神8447489 2022-11-01 16:12:02
我是 tkinter 新手并嘗試了一個簡單的 Tkinter GUI,但出現錯誤:TypeError:“NoneType”對象不可調用這是我的代碼:from tkinter import *from tkinter import filedialogimport 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():    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 回答

?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

'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... 而是給變量一個唯一的名稱



查看完整回答
反對 回復 2022-11-01
?
青春有我

TA貢獻1784條經驗 獲得超8個贊

這是答案:


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


查看完整回答
反對 回復 2022-11-01
?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

您所做的錯誤是一個錯字:通過編寫:

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

您將grid調用結果分配給原始 tk.Label 類型 ( Label)。網格回電是None

因此,當您嘗試創建 Label1 時,您實際上是在調用現在的 LabelNone

只需將行替換為:

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

或者干脆

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


查看完整回答
反對 回復 2022-11-01
  • 3 回答
  • 0 關注
  • 216 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號