簡體   English   中英

GUI,按鈕未顯示在窗口中

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM