![](/img/trans.png)
[英]I'm getting a weird error trying to run tests using pytest for FastAPI + Gino app
[英]I'm trying to develop tkinter app using oops but getting this error
我剛剛開始練習 Oops 概念。我正在觀看簡單的 Oops 視頻並嘗試對 tkinter 問題使用應用步驟。 我不知道為什么會收到此錯誤。
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
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
在沒有 arguments 的情況下調用__init__
function。 每次調用MyClass()
時,都會調用__init__
function ,因為 class 會被調用。
當您調用Framesone().stframe
時,代碼首先調用 Framesone() 的__init__()
Framesone()
,但沒有任何 arguments。 在聲明 object 本身之前,您正在使用對象的實例變量。
由於您已經在 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)
如果您確實想以現在使用的方式使用此stframe
,您可以將stframe
聲明移到__init__()
function 之外。 這會將stframe
從實例變量更改為 static 變量。 這將允許您從 class 外部調用Framesone.stframe
,而無需調用其構造函數。 (請注意,您現在調用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)
編輯:從 class static 變量中刪除self
。 為了更好地解釋實例與 static 變量,稍微更改了措辭
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.