Python:如何在消息框中获取输入框?

标签 python python-3.x tkinter messagebox

我刚刚意识到我没有在游戏开始时询问用户他们的名字。所以我尝试了这段代码:

 Label(root, text="What is you name?").grid(row=0, column=0)
 e1 = self.Entry(root)
 e1.self.grid(row = 0, column=1)

但它不起作用。

如何让游戏开始时弹出消息框“欢迎来到 Math bubbles,你叫什么名字?”然后用户输入他们的名字并按下确定。将弹出另一个消息框,上面写着“你好 _,请按开始按钮开始”。我想保存用户名,以便在获胜者消息框中也可以显示他们的名字

最佳答案

您可以使用tkinter.simpledialog函数askstring

from tkinter.simpledialog import askstring
from tkinter.messagebox import showinfo
name = askstring('Name', 'What is your name?')
showinfo('Hello!', 'Hi, {}'.format(name))

关于Python:如何在消息框中获取输入框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17779540/

相关文章:

python - Flask-mysql 编程错误 : not all arguments converted during string formatting

python - 使用 PIL.Image 和 ctypes 进行像素操作

Python 错误 : UnboundLocalError: local variable 'score1' referenced before assignment

python-3.x - 使用 OpenCV 或 PIL 将 Numpy 数组转换为图像

python - 如何在迭代列表时迭代其键时更改字典

python - Tkinter Linux 版本 "<Control-Shift-u>"

python - 使用 tkinter pack() 方法自动隐藏滚动条未按预期显示

python - Camelot python;OSError : exception: access violation writing 0x00000080

python - 无法在 zsh 上安装 python 包,但在 bash 上安装成功

python - 在 ubuntu 14.04 上安装 tkinter