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()
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__
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.