目录
2.编写带有图形化界面的猜数字游戏,数字为100以内整数。用户输入猜测值,如果正确,弹出“您猜对了!”消息框,否则提示“猜小了”或者“猜大了”消息框。
1.Tkinter实现用户登录界面。建立一个文本文件users.txt,其中每一行存储一个用户的名字和密码,二者之间使用冒号分割,例如“admin:123456”。用户输入名字和密码后,单击“Login”按钮,根据文件users.txt中存储的信息判断用户输入是否正确。如果不正确就提示“用户名或者密码不正确”,如果正确就提示“登录成功”。请将界面中的文字全部改为中文。
程序代码:
import tkinter
import tkinter.messagebox
with open("users.txt","w",encoding="gbk") as file:
file.write("用户名: ")
file.write("123456")
file.write("\n")
file.write("密码: ")
file.write("123456")
file.write("\n")
file.close()
root = tkinter.Tk()
root.title("登录界面")
root["height"] = 140
root["width"] = 220
labelName = tkinter.Label(root,text="用户名:",justify=tkinter.RIGHT,anchor='e',width=80)
labelName.place(x=0,y=10,width=80,height=20)
name = tkinter.StringVar(root,value='')
entername = tkinter.Entry(root,width=80,textvariable=name)
entername.place(x=90,y=10,width=80,height=20)
labelPwd = tkinter.Label(root,text="密码:",justify=tkinter.RIGHT,anchor='e',width=80)
labelPwd.place(x=0,y=45,width=80,height=20)
pwd = tkinter.StringVar(root,value='')
enterpwd = tkinter.Entry(root,show='*',width=80,textvariable=pwd)
enterpwd.place(x=90,y=45,width=80,height=20)
def login():
names = entername.get()
pwds = enterpwd.get()
number_list = []
with open("users.txt","r",encoding="gbk") as fp:
for number in fp:
number_list.append(number[6:12].strip().split("\n"))
fp.close()
if names == "" or pwds == "":
tkinter.messagebox.showwarning(title="错误",message="账号或者密码为空!")
elif names == number_list[0][0] and pwds == number_list[1][0]:
tkinter.messagebox.showinfo(title="恭喜",message="登陆成功!")
else:
tkinter.messagebox.showerror(title="警告",message="登陆失败!")
buttonOK = tkinter.Button(root,text="登录",command=login)
buttonOK.place(x=40,y=90,width=50,height=20)
def cancel():
name.set("")
pwd.set("")
buttoncancel = tkinter.Button(root,text="取消",command=cancel)
buttoncancel.place(x=120,y=90,width=50,height=20)
root.mainloop()
运行结果:
2.编写带有图形化界面的猜数字游戏,数字为100以内整数。用户输入猜测值,如果正确,弹出“您猜对了!”消息框,否则提示“猜小了”或者“猜大了”消息框。
程序代码:
import tkinter
import tkinter.messagebox
root = tkinter.Tk()
root["height"] = 180
root["width"] = 250
root.title("猜数游戏")
labelnumber = tkinter.Label(root,text="猜测数字:",justify=tkinter.RIGHT,anchor='e',width=80)
labelnumber.place(x=10,y=40,width=80,height=20)
number = tkinter.StringVar(root,value='')
enternumber = tkinter.Entry(root,width=80,textvariable=number)
enternumber.place(x=110,y=40,width=80,height=20)
def guess():
numbers = enternumber.get()
if numbers == "":
tkinter.messagebox.showerror(title="错误!",message="不能为空!")
if not numbers.isdigit():
tkinter.messagebox.showwarning(title="错误!", message="必须是数字!")
if int(numbers) < 0 or int(numbers)> 100:
tkinter.messagebox.showwarning(title="提醒",message="不能小于0或者大于100!")
elif numbers == "20":
tkinter.messagebox.showinfo(title="恭喜",message="猜对啦!")
elif numbers <"20":
tkinter.messagebox.showwarning(title="提醒",message="太小啦!")
elif numbers >"20":
tkinter.messagebox.showwarning(title="提醒",message="太大啦!")
guessbutton = tkinter.Button(root,text="确定",command=guess)
guessbutton.place(x=20,y=100,width=80,height=20)
def clear():
number.set("")
clarebutton = tkinter.Button(root,text="清空",command=clear)
clarebutton.place(x=140,y=100,width=80,height=20)
root.mainloop()
运行结果: