首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

#tkinter

phtnon中利用tkinter完成一个猜字游戏,遇到了以下问题?

// self多余 哎呀自己看吧 import random from tkinter import * from tkinter import messagebox, Button win = Tk() win.title('猜数字游戏') win.geometry('300x200') l = Label(win, text='来玩猜字谜游戏吧', bg='red', fg='black', height=2, width=20, font=('Arial', 12)) l.place(x=55, y=35) b = Button(win, text="开始", font=('Arial', 12), width=10, height=1, command=win.quit) b.place(x=100, y=100) win.mainloop() win.destroy() win = Tk() number = random.randint(0, 100) n = 0 win.title('猜数字游戏') win.geometry('300x200') l = Label(win, text='请输入你猜的数字', height=2, width=20, font=('Arial', 12)) l.place(x=55, y=25) l = Label(win, text='游戏规则:在1-100之间猜出正确数字', height=2, width=30, font=('Arial', 10)) l.place(x=30, y=150) e1 = Entry(win, width=20, ) e1.place(x=75, y=70) def guess(): global n t = e1.get() if int(t) == 0: messagebox.showinfo('提示', '请输入一个小于等于100的数') try: num = int(t) except ValueError: messagebox.showinfo('错误', '请输入一个小于等于100的数') return if num > number: n += 1 messagebox.showinfo('提示', '猜的数字大了,再小点!') e1.delete(0, END) return if num < number: n += 1 messagebox.showinfo('提示', '猜的数字小了,再大点!') e1.delete(0, END) return if num == number: messagebox.showinfo('提示', '恭喜你猜中了!!!,共猜测了{}次'.format(n)) return b= Button(win, text='确认', width=8, height=1, font=('Arial', 12), command=guess) b.place(x=105, y=110) win.mainloop() ... 展开详请
// self多余 哎呀自己看吧 import random from tkinter import * from tkinter import messagebox, Button win = Tk() win.title('猜数字游戏') win.geometry('300x200') l = Label(win, text='来玩猜字谜游戏吧', bg='red', fg='black', height=2, width=20, font=('Arial', 12)) l.place(x=55, y=35) b = Button(win, text="开始", font=('Arial', 12), width=10, height=1, command=win.quit) b.place(x=100, y=100) win.mainloop() win.destroy() win = Tk() number = random.randint(0, 100) n = 0 win.title('猜数字游戏') win.geometry('300x200') l = Label(win, text='请输入你猜的数字', height=2, width=20, font=('Arial', 12)) l.place(x=55, y=25) l = Label(win, text='游戏规则:在1-100之间猜出正确数字', height=2, width=30, font=('Arial', 10)) l.place(x=30, y=150) e1 = Entry(win, width=20, ) e1.place(x=75, y=70) def guess(): global n t = e1.get() if int(t) == 0: messagebox.showinfo('提示', '请输入一个小于等于100的数') try: num = int(t) except ValueError: messagebox.showinfo('错误', '请输入一个小于等于100的数') return if num > number: n += 1 messagebox.showinfo('提示', '猜的数字大了,再小点!') e1.delete(0, END) return if num < number: n += 1 messagebox.showinfo('提示', '猜的数字小了,再大点!') e1.delete(0, END) return if num == number: messagebox.showinfo('提示', '恭喜你猜中了!!!,共猜测了{}次'.format(n)) return b= Button(win, text='确认', width=8, height=1, font=('Arial', 12), command=guess) b.place(x=105, y=110) win.mainloop()
领券