猜數字的介面版

c_kin發表於2020-12-15
import tkinter
import tkinter.messagebox

root = tkinter.Tk()
root.title('猜數字')
shuzi=tkinter.StringVar()
Label = tkinter.Label(root,text="請輸入1到100以內的整數")
Label.place(x=20,y=10,width=140,height=40)


sg = tkinter.StringVar(root)
Entrysg =tkinter.Entry(root,width=50,textvariable=sg)
Entrysg.place(x=160,y=20,width=50,height=20)

def caishuzi():
    import random
    n=random.randint(1,100)
    i=5
    while i>0:
        x=int(Entrysg.get())
        if x < n:
            tkinter.messagebox.showinfo(title="結果",message="小了")
            i=i-1
        elif x> n:
            tkinter.messagebox.showinfo(title="結果",message="大了")
            i=i-1
        else:
            tkinter.messagebox.showinfo(title="結果",message="恭喜你猜對了")
            break
    else:
        tkinter.messagebox.showinfo(title="結果",message="次數已用完")
        
    return
    
 

button =tkinter.Button(root,text="測試",command=caishuzi)
button.place(x=100,y=70,width=30,height=20)
root.mainloop()

相關文章