13 图形界面(GUI)及猜数字游戏
1 GUI:Graphical User Interface
2 tkinter:GUI library for Python
3 GUI Example
13.1 图形界面
from
tkinter
import
*
import
tkinter.messagebox
as
mb
import
tkinter.simpledialog
as
dl
root=Tk()
w=Label(root,text="Label")
w.pack()
mb.showinfo("welcome",
"welcome learn
Python!")
guess=dl.askinteger("Number",
"please input an number:")
mb.showinfo("output:",guess)
13.2 猜数字游戏
from
tkinter
import
*
import
tkinter.messagebox
as
mb
import
tkinter.simpledialog
as
dl
root=Tk()
number=66
guessFlag=False
while
guessFlag==False:
guess=dl.askinteger("Number
Game",
"please input the number:")
if
guess==number:
mb.showinfo("Congratulation",
"You win!")
guessFlag=True
elif
guess>number:
mb.showinfo("Sorry","The
number you input is bigger than true number!")
else:
mb.showinfo("Sorry","The
number you input is smaller than true number!")
本文介绍了如何使用Python的tkinter库创建简单的图形用户界面(GUI),并通过实例演示了一个猜数字游戏的应用程序。游戏通过对话框提示用户输入数字,并根据用户输入给出反馈。
158

被折叠的 条评论
为什么被折叠?



