Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类型错误:缺少多个.py文件所需的位置参数_Python_Python 3.x_Tkinter_Python Curses - Fatal编程技术网

Python 类型错误:缺少多个.py文件所需的位置参数

Python 类型错误:缺少多个.py文件所需的位置参数,python,python-3.x,tkinter,python-curses,Python,Python 3.x,Tkinter,Python Curses,我想创建一个类,然后创建一个对象,它将在我创建的显示器中显示一个红方块 此时,我只能显示蓝色正方形,它不是“Baustein”类的对象 以下是我现在使用的两个文件: Bauklotz_class.py from gui_backup import Display class Baustein: x1, y1, x2, y2 = 10,10,20,20 color = "red" def __init__(self, x1, y1, x2, y2, color):

我想创建一个类,然后创建一个对象,它将在我创建的显示器中显示一个红方块

此时,我只能显示蓝色正方形,它不是“Baustein”类的对象

以下是我现在使用的两个文件:

Bauklotz_class.py

from gui_backup import Display
class Baustein:
    x1, y1, x2, y2 = 10,10,20,20
    color = "red"
    def __init__(self, x1, y1, x2, y2, color):
        self.x1 = x1
        self.x2 = x2
        self.y1 = y1
        self.y2 = y2
        self.color = color
    def show_new_object(self):
        quadrat2 = Display.create_rectangle(40, 50, 60, 70, fill = color)
        Display.coords(quadrat2, x1, y1, x2, y2)
gui_backup.py

from tkinter import *
import curses
import Bauklotz_class

x1 = 10   #initialise coordinates
y1 = 10
x2 = 20
y2 = 20

root = Tk() #create window
root.wm_title("Raspberry Pi GUI") #window title
root.config(background = "#FFFFFF") #background color

#The whole left frame and widgets involved
leftFrame = Frame(root, width=200, height = 400)
leftFrame.grid(row=0, column = 0, padx = 10, pady = 3)
leftLabel1 = Label(leftFrame, text = "Platzhalter Text")
leftLabel1.grid(row = 0, column = 0, padx = 10, pady = 3)
leftLabel2 = Label(leftFrame, text = "Dies ist ein Text\nmit mehreren Zeilen")
leftLabel2.grid(row = 1, column = 0, padx = 10, pady = 3)


#the whole right frame and widgets involved
rightFrame = Frame(root, width=400, height = 400)
rightFrame.grid(row = 0, column = 1, padx = 10, pady = 3)
E1 = Entry(rightFrame, width = 50)
E1.grid(row = 0, column = 0, padx = 10, pady = 60)

#The two functions for the 2 buttons created
def callback1():
    test = Bauklotz_class.Baustein(20, 30, 40, 50, "red")
    test.show_new_object()

def callback2():
    print(1+1)

buttonFrame = Frame(rightFrame)
buttonFrame.grid(row = 1, column = 0, padx = 10, pady = 60)
B1 = Button(buttonFrame, text = "Button1", bg = "#FF0000", width = 15, command = callback1)
B1.grid(row = 0, column = 0, padx = 10, pady = 60)
B2 = Button(buttonFrame, text = "Button2", bg ="#FFFF00", width = 15, command = callback2)
B2.grid(row = 0, column = 1, padx = 10, pady = 60)

Slider = Scale(rightFrame, from_ = 0, to = 100, resolution = 0.1, orient = HORIZONTAL, length = 400)
Slider.grid(row = 2, column = 0, padx = 10, pady = 3)

Display = Canvas(rightFrame, width = 300, height = 300)
Display.configure(background = 'black')
Display.grid(row = 1, column = 3, padx = 10, pady = 3)

quadrat = Display.create_rectangle(20, 30, 40, 50, fill = "blue")
Display.coords(quadrat, x1, y1, x2, y2)


#following functions are for coordination of the square
#also you can find here the exceptions so that the object
#cant break out of the display widget

def down(event):
    global x1, y1, x2, y2
    if x2 == 290 or y2 == 300:
        pass
    else:
        y1 += 10
        y2 += 10
        Display.coords(quadrat, x1, y1, x2, y2)
        leftLabel1.config(text = "x1: " + str(x1) + ", x2:" + str(x2) + ", y1:" + str(y1) + ", y2:" + str(y2), width = "40" , )
def up(event):
    global x1, y1, x2, y2
    if x2 == 0 or y2 == 10:
        pass
    else:
        y1 -= 10
        y2 -= 10
        Display.coords(quadrat, x1, y1, x2, y2)
        leftLabel1.config(text = "x1: " + str(x1) + ", x2:" + str(x2) + ", y1:" + str(y1) + ", y2:" + str(y2), width = "40" , )
def left(event):
    global x1, y1, x2, y2
    if x1 == 0 or y1 == 10:
        pass
    else:
        x1 -= 10
        x2 -= 10
        Display.coords(quadrat, x1, y1, x2, y2)
        leftLabel1.config(text = "x1: " + str(x1) + ", x2:" + str(x2) + ", y1:" + str(y1) + ", y2:" + str(y2), width = "40" , )
def right(event):
    global x1, y1, x2, y2
    if x1 == 290 or y1 == 300:
        pass
    else:
        x1 += 10
        x2 += 10
        Display.coords(quadrat, x1, y1, x2, y2)
        leftLabel1.config(text = "x1: " + str(x1) + ", x2:" + str(x2) + ", y1:" + str(y1) + ", y2:" + str(y2), width = "40" , )    
root.bind('<a>', left)
root.bind('<w>', up)
root.bind('<s>', down)
root.bind('<d>', right)


root.mainloop()
从tkinter导入*
进口诅咒
导入Bauklotz_类
x1=10#初始化坐标
y1=10
x2=20
y2=20
root=Tk()#创建窗口
root.wm_标题(“覆盆子皮GUI”)#窗口标题
root.config(background=“#ffffffff”)#背景色
#整个左侧框架和涉及的小部件
leftFrame=Frame(根,宽度=200,高度=400)
leftFrame.grid(行=0,列=0,padx=10,pady=3)
leftLabel1=标签(leftFrame,text=“Platzhalter text”)
leftLabel1.grid(行=0,列=0,padx=10,pady=3)
leftLabel2=标签(leftFrame,text=“deis ist ein text\nmit mehreren Zeilen”)
leftLabel2.grid(行=1,列=0,padx=10,pady=3)
#整个右框架和涉及的小部件
rightFrame=Frame(根,宽度=400,高度=400)
rightFrame.grid(行=0,列=1,padx=10,pady=3)
E1=入口(右框,宽度=50)
E1.网格(行=0,列=0,padx=10,pady=60)
#创建的两个按钮的两个功能
def callback1():
测试=Bauklotz_类Baustein(20,30,40,50,“红色”)
test.show_new_object()
def callback2():
打印(1+1)
按钮框架=框架(右框架)
buttonFrame.grid(行=1,列=0,padx=10,pady=60)
B1=按钮(按钮框架,text=“Button1”,bg=“#FF0000”,宽度=15,命令=callback1)
B1.网格(行=0,列=0,padx=10,pady=60)
B2=按钮(按钮框架,text=“Button2”,bg=“#FFFF00”,宽度=15,命令=callback2)
B2.网格(行=0,列=1,padx=10,pady=60)
滑块=比例(右框,从0到100,分辨率=0.1,方向=水平,长度=400)
Slider.grid(行=2,列=0,padx=10,pady=3)
显示=画布(右框,宽度=300,高度=300)
Display.configure(背景='black')
Display.grid(行=1,列=3,padx=10,pady=3)
样方=显示。创建矩形(20,30,40,50,fill=“蓝色”)
显示坐标(样方、x1、y1、x2、y2)
#以下功能用于协调广场
#您还可以在这里找到异常,以便
#无法中断显示小部件
def关闭(事件):
全局x1、y1、x2、y2
如果x2==290或y2==300:
通过
其他:
y1+=10
y2+=10
显示坐标(样方、x1、y1、x2、y2)
leftLabel1.config(text=“x1:”+str(x1)+”,x2:“+str(x2)+”,y1:“+str(y1)+”,y2:“+str(y2),width=“40”,)
def up(事件):
全局x1、y1、x2、y2
如果x2==0或y2==10:
通过
其他:
y1-=10
y2-=10
显示坐标(样方、x1、y1、x2、y2)
leftLabel1.config(text=“x1:”+str(x1)+”,x2:“+str(x2)+”,y1:“+str(y1)+”,y2:“+str(y2),width=“40”,)
def left(事件):
全局x1、y1、x2、y2
如果x1==0或y1==10:
通过
其他:
x1-=10
x2-=10
显示坐标(样方、x1、y1、x2、y2)
leftLabel1.config(text=“x1:”+str(x1)+”,x2:“+str(x2)+”,y1:“+str(y1)+”,y2:“+str(y2),width=“40”,)
def权限(事件):
全局x1、y1、x2、y2
如果x1==290或y1==300:
通过
其他:
x1+=10
x2+=10
显示坐标(样方、x1、y1、x2、y2)
leftLabel1.config(text=“x1:”+str(x1)+”,x2:“+str(x2)+”,y1:“+str(y1)+”,y2:“+str(y2),width=“40”,)
root.bind(“”,左)
root.bind(“”,向上)
root.bind(“”,向下)
root.bind(“”,右)
root.mainloop()
现在我只有一个问题,就是生成了以下错误:AttributeError:module'Bauklotz_class'没有属性'Baustein'。我真的搞不懂python是什么意思,我也是OOP的新手,尤其是python。有人能帮我解决这个问题吗

以下是我收到的完整错误消息:

Tkinter回调回溯中出现异常(最近一次调用最后一次):
文件“/usr/lib/python3.5/tkinter/init.py”,第1562行,在调用中 返回self.func(*args)文件“/home/pi/Documents/TKinter_220;bung/gui_220;backup.py”,第31行,在 回拨1 test=Bauklotz_类。Baustein(20,30,40,50,“红色”)属性错误:模块“Bauklotz_类”没有属性“Baustein”


Callback1函数中的这一行:

 test = Bauklotz_class.Baustein()
def __init__(self, x1, y1, x2, y2, color):
括号中需要5个值(“x1”、“y1”、“x2”、“y2”和“颜色”) 因为它正在调用baustein类,该类在init函数中请求这些参数:

 test = Bauklotz_class.Baustein()
def __init__(self, x1, y1, x2, y2, color):

非常感谢你!现在我有一条新的错误消息:TypeError:show_new_object()接受0个位置参数,但给出了1个。我真的不明白为什么会这样,我没有在test.show_new_object()的括号中写任何东西,但是python想告诉我1个参数是给定的每个方法(类中的函数)都需要self作为第一个参数。调用函数时不需要添加self,但在函数定义中需要它。更改def show_new_object():以定义show_new_object(self):在您的baustein类中,我以前尝试过此操作,但收到错误消息:NameError:name'self'未定义,并且我只添加了您刚才所说的“self”,因此现在我在baustein_类中的定义如下:def show_new_object(self)是的,它不再显示错误!非常感谢,但我有一个新的错误。名称错误:未定义名称“显示”。这是指baucklotz_class.py中的第11行,这也是我遇到的一个问题。我如何告诉python这个显示是来自gui_backup.py的显示?这应该是我的最后一个问题。假设backup.py是您计算机上的本地文件(由您或您的老师编程),并且与您可以在bauklotz.py中作为第一行写入的其他两个文件位于同一文件夹中:“from gui_backup import Display”您的
def show_new_