0

For my homework, I have to create a widget. I'm very new to this. I'm trying to get my buttons to show up. I have tried packing them, but I don't know what I'm doing wrong. Here is what I have.

from tkinter import *
import turtle
main = Tk()
main.title("TurtleApp")

class turtleApp:

  def __init_(self):
    self.main = main
    self.step = 10
    self.turtle = turtle.Turtle()
    self.window = turtle.Screen()
    self.createDirectionPad

  def createDirectionPad(self):
    mainFrame = Frame(main)
    mainFrame.pack()
    button1 = Button(mainFrame,text = "left", fg="red")
    button2 = Button(mainFrame,text = "right", fg="red")
    button3 = Button(mainFrame,text = "up", fg="red")
    button4= Button(mainFrame,text = "down", fg="red")
    button1.pack()
    button2.pack()
    button3.pack()
    button4.pack()

main.mainloop()

1 Answer 1

2

First of all your indentation is off, but once you fix that, you never actually create an instance of your turtleApp class, so none of that code ever gets executed leaving you with an empty GUI.

# Actually create a turtleApp instance which adds the buttons
app = turtleApp()

# Enter your main event loop
main.mainloop()

You also need to actually call createDirectionPad within __init__ using explicit (). As it is, self.createDirectionPad (without the ()) just creates a reference to the method and doesn't actually call it.

def __init__(self):
    # other stuff
    self.createDirectionPad()

Update

You also have a typo in your __init__ function declaration. You are missing the final _ in __init__

Sign up to request clarification or add additional context in comments.

8 Comments

it's still not running after I create the instance
@Mia Updated above
I saw and fixed that after I posted the code. Apparently, there's another problem in my code that I'm not seeing.
@Mia Did you see my update about calling createDirectionPad() ?
@Mia What is window? that isn't defined.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.