【问题标题】:TypeError: PNI() missing 1 required positional argument: 'self'TypeError:PNI() 缺少 1 个必需的位置参数:'self'
【发布时间】:2022-01-05 11:39:03
【问题描述】:

对于这个问题的任何帮助将不胜感激。 我不断收到以下错误,不知道为什么。

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Beast\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
TypeError: PNI() missing 1 required positional argument: 'self'

下面是我到目前为止的代码。 据我所知,问题是由 def PNI 函数引起的。 我已经尝试过该功能是一个简化的程序,并且它可以正常工作,因此非常感谢任何帮助使其在这里工作。

from tkinter import *
import pandas as pd
import os
import shutil
import time

#Font sizes
LARGE_FONT = ("Verdana", 12)

#Main frame
class IntegrationAPP(Tk):

    def __init__(self, *args, **kwargs):
    
        Tk.__init__(self, *args, **kwargs)
        container = Frame(self)
        
        container.pack(side="top", fill="both", expand = True)
        
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        
        self.frames = {}
        
        for F in (StartPage, Test):
            
            frame = F(container, self)
            
            self.frames[F] = frame
            
            frame.grid(row=0, column=0, sticky="nsew")
        
        self.show_frame(StartPage)
        
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class StartPage(Frame):

    def __init__(self, parent, controller, *args, **kwargs):
        Frame.__init__(self, parent)

        #Create entry feild
        pni = Entry(self, width=50)
        pni.insert(0, 'Enter project number')

        #Pull data from entry feild
        def PNI(self, *args, **kwargs):
            label1 = Label(self, text = pni.get())
            label1.grid(row=0, column=2)

        #Create submit button
        buttonpni = Button(self, text="Submit", command=PNI)

        #Location in grid
        pni.grid(row=0, column=0)
        buttonpni.grid(row=1, column=0)

#Future epansion
class Test(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self,parent)
        label1 = Label(self, text="Hello", font=LARGE_FONT)
        
        label1.grid(row=0, column=0)

app = IntegrationAPP()
app.mainloop()

【问题讨论】:

  • PNI() 的缩进不正确。

标签: python function class tkinter


【解决方案1】:

把它移到左边,目前它在 init 函数里面。基本上只是一个缩进问题。通常大多数 IDE 都允许您使用 ctrl+{ 或 ctrl+}

左右移动选定的代码
 #Pull data from entry feild
    def PNI(self, *args, **kwargs):
        label1 = Label(self, text = pni.get())
        label1.grid(row=0, column=2)

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 2023-02-06
    • 2021-09-15
    • 2013-07-06
    • 2017-03-28
    • 2020-01-07
    • 2019-09-26
    • 2017-02-23
    相关资源
    最近更新 更多