简体   繁体   English

我正在尝试使用 oops 开发 tkinter 应用程序但收到此错误

[英]I'm trying to develop tkinter app using oops but getting this error

I have just started to practice Oops concepts.I'm watching simple Oops video and trying too use apply steps to the tkinter problrm.我刚刚开始练习 Oops 概念。我正在观看简单的 Oops 视频并尝试对 tkinter 问题使用应用步骤。 I don't why I'm getting this error.我不知道为什么会收到此错误。

from tkinter import *
from tkinter import font as tkFont

top = Tk()
top.minsize(width=1280,height=720)
top.maxsize(width=721,height=521)
class Framesone:
    def __init__(self, x1, y1, frame1, text1, x2, y2):
        self.stframe = LabelFrame(top, width=300, height=200, highlightcolor="grey", bd=5)
        self.stframe.place(x=x1, y=y1)
        self.label1 = Label(frame1, text=text1)
        self.label1.config(font=("Times", "25", "bold", "italic"))
        self.label1.place(x=x2, y=y2)
Framesone(100,200,Framesone().stframe,"HI",20,30)
top.mainloop()

OUTPUT OUTPUT

Traceback (most recent call last):
  File "E:/python projects my/Basic Programs/MQC FIt Software.py", line 14, in <module>
    Framesone(100,200,Framesone().stframe,"HI",20,30)
TypeError: __init__() missing 6 required positional arguments: 'x1', 'y1', 'frame1', 'text1', 'x2', and 'y2'

Process finished with exit code 1
Framesone(100,200,Framesone().stframe,"HI",20,30)

Framesone().stframe calls the __init__ function without arguments. Framesone().stframe在没有 arguments 的情况下调用__init__ function。 Everytime you call MyClass() the __init__ function for that class gets called.每次调用MyClass()时,都会调用__init__ function ,因为 class 会被调用。

When you're calling Framesone().stframe the code first calls the __init__() function of Framesone() , but without any arguments.当您调用Framesone().stframe时,代码首先调用 Framesone() 的__init__() Framesone() ,但没有任何 arguments。 You're using the object's instance variables before you have declared the object itself.在声明 object 本身之前,您正在使用对象的实例变量。

As you've defined the stframe already in your object you could simply replace references to frame1 with self.stframe as in your example these are the same.由于您已经在 object 中定义了stframe ,您可以简单地用self.stframe替换对frame1的引用,因为在您的示例中这些是相同的。

class Framesone:
    def __init__(self, x1, y1, text1, x2, y2):
        self.stframe = LabelFrame(top, width=300, height=200, highlightcolor="grey", bd=5)
        self.stframe.place(x=x1, y=y1)
        self.label1 = Label(self.stframe, text=text1)
        self.label1.config(font=("Times", "25", "bold", "italic"))
        self.label1.place(x=x2, y=y2)

Framesone(100, 200, "HI", 20, 30)

If you do want to use this stframe the way you're using it now, you could move the stframe declaration outside of the __init__() function.如果您确实想以现在使用的方式使用此stframe ,您可以将stframe声明移到__init__() function 之外。 This would change stframe from being an instance variable to being a static variable.这会将stframe从实例变量更改为 static 变量。 This would allow you to call Framesone.stframe from outside your class without calling its constructor.这将允许您从 class 外部调用Framesone.stframe ,而无需调用其构造函数。 (Note you're now calling Framesone without the () indicating you're using its static class variable instead of an instance variable.) (请注意,您现在调用Framesone没有()表示您正在使用其 static class 变量而不是实例变量。)

class Framesone:
    stframe = LabelFrame(top, width=300, height=200, highlightcolor="grey", bd=5)
    
    def __init__(self, x1, y1, frame1, text1, x2, y2):
        self.stframe.place(x=x1, y=y1)
        self.label1 = Label(self.frame1, text=text1)
        self.label1.config(font=("Times", "25", "bold", "italic"))
        self.label1.place(x=x2, y=y2)

Framesone(100, 200, Framesone.stframe, "HI", 20, 30)

Edit: removed self from class static variable.编辑:从 class static 变量中删除self Changed wording slightly for better explanation of instance vs static variable为了更好地解释实例与 static 变量,稍微更改了措辞

暂无
暂无

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

相关问题 我在尝试使用 pytest for FastAPI + Gino 应用程序运行测试时遇到一个奇怪的错误 - I'm getting a weird error trying to run tests using pytest for FastAPI + Gino app 我正在尝试在 ubuntu 上的崇高文本中使用 python 上的 tkinter 但我一直收到错误 - I'm trying to use tkinter on python in sublime text on ubuntu but i keep getting a error 我在使用tkinter时遇到此属性错误 - I'm getting this attribute error working with tkinter 我在 python 中收到此错误:“AttributeError: 'str' object has no attribute 'set'”。 我正在使用 tkinter,我该如何解决这个问题? - I'm getting this error in python: “AttributeError: 'str' object has no attribute 'set'”. I'm using tkinter, how do I fix this? 我正在尝试使用 keras 库训练模型,但出现值错误 - I'm trying to train the model using keras library but I'm getting value error 我正在尝试使用 sqlalchemy 连接到我机器上的 SQL Express 服务器,但出现错误 - I'm trying to connect to a SQL Express Server on my machine using sqlalchemy but I'm getting an error 我正在尝试使用python实现strassen的算法,但出现错误 - I'm trying to implement strassen's algorithm using python and I'm getting a error 我试图理解为什么在使用 paramiko 1.7.6 时会出现“Permission Denied”错误 - I'm trying to understand why I'm getting a “Permission Denied” error when using paramiko 1.7.6 当我尝试在 VsCode 上使用 Tkinter 时,我收到一个 NameError 说名称“Tk”未定义 - I'm getting a NameError saying name 'Tk' is not defined when I'm trying to use Tkinter on VsCode 为什么我得到 object has no attribute error in python tkinter - Why I'm getting object has no attribute error in python tkinter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM