简体   繁体   English

Python tkinter,缺少位置参数错误

[英]Python tkinter, missing positional argument error

I am trying to develop a simple python tool using Tkinter.I have created 3 files and below is the code.我正在尝试使用 Tkinter 开发一个简单的 python 工具。我创建了 3 个文件,下面是代码。 The files are GUI.py这些文件是 GUI.py

from tkinter import *
from tkinter.messagebox import *
import tkinter.ttk as ttk
import versionTab
import logclass

class GUI(Tk):
    def __init__(self, parent):
        self.parent = parent
        parent.title("Tool")
        parent.geometry('695x600+100+100')
        self.frame = Frame(parent,bd=5).grid()
        self.infoLabel = LabelFrame(self.frame,text="Information",height=100,width=100)
        self.version = versionTab.versionTab(self.infoLabel)
        self.infoLabel.grid(row=1,column=0,columnspan =2,padx=8,pady=2,sticky = W)

        self.logLabel = LabelFrame(self.frame,height=1000,width=1000)
        self.Log = logclass.debuglog(self.logLabel)
        self.logLabel.grid(row=4,column=0,columnspan =2,padx=8,pady=2,sticky = W)

root = Tk()
gui = GUI(root)
root.resizable(0,0)
root.mainloop()

The next file is versionTab.py下一个文件是 versionTab.py

from tkinter import *
from tkinter.messagebox import *
import logclass

class versionTab():
    def __init__(self,frame):
        self.firmwareFrame =LabelFrame(frame,height=15,width=100)
        self.firmwareversion_B = Button(self.firmwareFrame, text=" Version",command= self.getFW).grid(row=0,column=0,padx=2,pady=5)
        self.versionText_Entry = Entry(self.firmwareFrame,width=10)
        self.versionText_Entry.configure(state ='readonly')
        self.versionText_Entry.grid(row=0,column=1,padx=4,pady=5)
        self.firmwareFrame.grid(row=1,column=0,columnspan =2,padx=2,pady=2,sticky = W)

    def getFW(self):
        logclass.debuglog.logst(self,"12344")

final file is logclass.py最终文件是 logclass.py

from tkinter import *
import tkinter.ttk as ttk
from datetime import datetime

class debuglog():
    def __init__(self,frame):
        self.debuglog = LabelFrame(frame,text = "Log",height=15,width=100)
        self.log      = Text(self.debuglog,height = 20, width = 80)
        self.scrollb  = Scrollbar(self.debuglog, orient=VERTICAL)
        self.scrollb.config(command = self.log.yview) 
        self.log.config(yscrollcommand = self.scrollb.set)
        self.log.grid(column=0, row=0)
        self.scrollb.grid(column=1, row=0, sticky=W)
        self.debuglog.pack()

    def logst(self,msg, level=None):
        print('logclass')
        self.log.insert(insert('end', msg + '\n'))

    def exitWindow(self):

        print('exit..')

while I are running my GUI.py file I am able to generate the tool.The tool will display a simple button version.when I click the version button on tool its throwing the below error当我运行我的 GUI.py 文件时,我能够生成该工具。该工具将显示一个简单的按钮版本。当我单击工具上的版本按钮时,它会引发以下错误

versionTab.py", line 15, in getFW
    logclass.debuglog.logst(self)
TypeError: logst() missing 1 required positional argument: 'msg'

Could any one tell me why I am not able to call the logst from my versiontab file谁能告诉我为什么我不能从我的 versiontab 文件中调用 logst

You seem to be attempting to call the method logst on the class debuglog .您似乎试图在 class debuglog上调用方法logst This will not work, as "logst" is not a static method.这不起作用,因为“logst”不是 static 方法。 You would need to create an instance of debuglog in order to call the method logst on it.您需要创建一个debuglog实例才能在其上调用方法logst

I also do not understand why you are passing self to the logst function when you call it我也不明白你为什么在调用它时将self传递给logst

In the constructor for the versionTab class, you could add self._logger = logclass.debuglog() , and then call self._logger.logst rather than logclass.debuglog.logst .versionTab class 的构造函数中,您可以添加self._logger = logclass.debuglog() ,然后调用self._logger.logst而不是logclass.debuglog.logst

Perhaps look into the differences between static methods and non static methods?也许看看 static 方法和非 static 方法之间的区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM