Python中使用turtle的警报框

问题描述 投票:0回答:2

如何在运行使用海龟图形的程序时出现警告框?我们有 textinput() 但它要求用户输入一些内容,但我只想在那里显示一些文本。海龟图形可以吗?

python turtle-graphics python-turtle
2个回答
2
投票

试试这个:

from tkinter import * 
from tkinter import messagebox
  
root = Tk()
  
messagebox.showinfo("showinfo", "Information") # The alert.
  
root.mainloop() 

0
投票

我发现 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()
© www.soinside.com 2019 - 2024. All rights reserved.