[英]GUI, Buttons not displaying in window
对于我的作业,我必须创建一个小部件。 我对此很陌生。 我试图让我的按钮显示出来。 我已经尝试打包它们,但是我不知道自己在做什么错。 这就是我所拥有的。
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()
首先,您的缩进是关闭的,但是一旦解决,就不会真正创建turtleApp
类的实例,因此该代码都不会执行,而给您留下空的GUI。
# Actually create a turtleApp instance which adds the buttons
app = turtleApp()
# Enter your main event loop
main.mainloop()
您还需要实际使用__init__
调用 createDirectionPad
()
。 实际上, self.createDirectionPad
(不带()
)仅创建对该方法的引用,而实际上并未调用它。
def __init__(self):
# other stuff
self.createDirectionPad()
更新资料
您在__init__
函数声明中也有错字。 您在__init__
中缺少最后一个_
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.