所以我遵循教程Intro to Tkinter并在复制源代码时,它在我运行程序时不起作用。我读了我的语法,搜索了视频上的评论,堆栈溢出,我找不到解决方案。
import Tkinter
import turtle
import sys
def main():
root = Tkinter.Tk()
cv = Tkinter.Canvas(root, width = 600, height= 600)
cv.pack(side = Tkinter.LEFT)
root.title("Draw")
t = turtle.RawTurtle(cv)
screen = t.getscreen()
screen.setworldcoordinates(0,0,600,600)
frame = Tkinter.Frame(root)
frame.pack(side = Tkinter.RIGHT, fill = Tkinter.BOTH)
def quithandler():
print 'Goodbye'
sys.exit(0)
quitbutton = Tkinter.Button(frame, text='Quit', command = quithandler)
quitbutton.pack()
if __name__ == "__main__":
main()
我也在Windows上运行python 2.7。在此程序中,退出按钮不会显示,并且画布在我运行时不会立即响应。是什么导致它每次都这样做?
感谢您的帮助。
答案 0 :(得分:2)
正确缩进。 +你错过了root.mainloop()
电话。
import Tkinter
import turtle
import sys
def main():
root = Tkinter.Tk()
cv = Tkinter.Canvas(root, width = 600, height= 600)
cv.pack(side = Tkinter.LEFT)
root.title("Draw")
t = turtle.RawTurtle(cv)
screen = t.getscreen()
screen.setworldcoordinates(0,0,600,600)
frame = Tkinter.Frame(root)
frame.pack(side = Tkinter.RIGHT, fill = Tkinter.BOTH)
quitbutton = Tkinter.Button(frame, text='Quit', command = quithandler)
quitbutton.pack()
root.mainloop()
def quithandler():
print 'Goodbye'
sys.exit(0)
if __name__ == "__main__":
main()