0

I want to create a class and then an object which will display a red square in my display i also created.

At this moment, I can only display the blue square, which is not an object of the class "Baustein".

Here are my 2 files im using right now:

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()

Now I only have the problem, that the following error is beeing generated: AttributeError: module 'Bauklotz_class' has no attribute 'Baustein'. I cant really figure out what python means by this, Im also a newbie in OOP, especialy in Python. Can someone help me with this problem?

Here is the full error message i get:

Exception in Tkinter callback Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/init.py", line 1562, in call return self.func(*args) File "/home/pi/Documents/TKinter_Übung/gui_backup.py", line 31, in callback1 test = Bauklotz_class.Baustein(20, 30, 40, 50, "red") AttributeError: module 'Bauklotz_class' has no attribute 'Baustein'

  • 1
    Your `def show_new_object()` is missing the first argument to refer to the object (i.e. `self`). – TrebledJ Jan 11 '19 at 10:14
  • but I am not using self in my show_new_object() function, and only if i paste self into the brackets of show_new_object() it cant help me – Kubaguette The Testobun Jan 11 '19 at 10:18
  • 1
    In `test.show_new_object()`, Python automatically provided the `self` argument to your method as `test` whenever you call it as a method. You need an argument to `show_new_object()` to be able to call it as a method from objects of your Baustein class. – TrebledJ Jan 11 '19 at 10:24

1 Answers1

1

This line in your Callback1 function:

 test = Bauklotz_class.Baustein()

needs 5 values in the brackets ('x1', 'y1', 'x2', 'y2', and 'color') because it is calling your baustein class which ask for those parameters in the init function:

def __init__(self, x1, y1, x2, y2, color):
Florian H
  • 3,052
  • 2
  • 14
  • 25
  • Thank you very much! Now I have a new error message: TypeError: show_new_object() takes 0 positional arguments but 1 was given. I dont really understand why it is so, I am not writing anything into the brackets of test.show_new_object(), but python wants to tell me that 1 argument was given – Kubaguette The Testobun Jan 11 '19 at 10:14
  • 1
    every method (function in a class) needs self as first argument. you dont need to add self when you call the function but you need it in the function definition. Change def show_new_object(): to def show_new_object(self): in your baustein class – Florian H Jan 11 '19 at 10:21
  • I tried this before but I get the error message: NameError: name 'self' is not defined, and I added only "self" just you said, so that now my definition in the baustein_class looks like this: def show_new_object(self) – Kubaguette The Testobun Jan 11 '19 at 10:26
  • Yes, it doesnt show the error anymore! Thank you very much, but I have a new error. NameError: name 'Display' is not defined. This is refering to line 11 in the baucklotz_class.py and that is also a problem I had. How can I tell python that this Display is the Display from gui_backup.py? This should be my final question. – Kubaguette The Testobun Jan 11 '19 at 10:37
  • 1
    assuming backup.py is a local file on your computer (programmed by you or your teacher) and is in the same folder as the other two files you can write as first line in bauklotz.py: "from gui_backup import Display" – Florian H Jan 11 '19 at 10:41
  • Another error occured. This time in gui_backup line 31. AttributeError: module 'Bauklotz_class' has no attribute 'Baustein'. Im really curious at this point, "Baustein" should not be a attribute, Baustein is my class. – Kubaguette The Testobun Jan 11 '19 at 10:47
  • can you edit the question ant post your actual files? – Florian H Jan 11 '19 at 11:09
  • Yes sir, I updated it now. Now you can see my actual files. – Kubaguette The Testobun Jan 11 '19 at 11:14
  • Ok i fixed it now. Onyl thing I needed to do was to import Bauklotz_class directly in callback1(), I found out that it makes a difference where you import modules. Thank you for the help buddy! – Kubaguette The Testobun Jan 11 '19 at 11:41