如何在运行使用海龟图形的程序时出现警告框?我们有 textinput() 但它要求用户输入一些内容,但我只想在那里显示一些文本。海龟图形可以吗?
试试这个:
from tkinter import *
from tkinter import messagebox
root = Tk()
messagebox.showinfo("showinfo", "Information") # The alert.
root.mainloop()
我发现 SecretLloyd 的答案有助于找到我需要的东西,但它也会导致在 Turtle 窗口旁边的屏幕上加载一个额外的空 Tk 窗口。
再深入一点,
turtle
模块导入了tkinter as TK
,因此我们可以通过turtle模块的引用来调用消息框,而不是单独导入tkinter
from turtle import Turtle, Screen, TK
t = Turtle()
t.shape("turtle")
t.color("green")
t.forward(100)
TK.messagebox.showinfo(title="The Turtle says:", message="Time for a nap!")
Screen().exitonclick()
或者:
import turtle
t = turtle.Turtle()
t.shape("turtle")
t.color("green")
t.forward(100)
turtle.TK.messagebox.showinfo(title="The Turtle says:", message="Time for a nap!")
turtle.exitonclick()