iT邦幫忙

0

python 猜數字遊戲按下開始就沒有回應了

  • 分享至 

  • xImage

程式一定有錯但是我不知道在哪裡請各位大神幫幫我

import tkinter as tk
import random

def play():  
    rankc = int(rank_en.get())
    
    if rankc == 1 : 
       easy(rank1)
    if rankc == 2 :
        normal(rank1)
    if rankc == 3 :
        hard(rank1)
        
def easy(rank):
    n = random.randint(1,10)
    a = 0
    while True:
        a = a+1
        gue = gus_en.get()
        if gue.isdigit():
            gue = int(gue)
            if gue > n:
                print(gue,"太大了")
            elif gue < n:
                print(gue,"太小了")
            else :
                print("恭喜你猜對了,你猜了",a,'次')
                break

def normal(rank):
    n = random.randint(1,100)
    a = 0
    while True:
        a = a+1
        gue = gus_en.get()
        if gue.isdigit():
            gue = int(gue)
            if gue > n:
                print(gue,"太大了")
            elif gue < n:
                print(gue,"太小了")
            else :
                print("恭喜你猜對了,你猜了",a,'次')
                break

def hard(rank):
    n = random.randint(1,200)
    a = 0
    while True:
        a = a+1
        gue = gus_en.get()
        if gue.isdigit():
            gue = int(gue)
            if gue > n:
                print(gue,"太大了")
            elif gue < n:
                print(gue,"太小了")
            else :
                print("恭喜你猜對了,你猜了",a,'次')
                break
rank1 = []
win = tk.Tk()

win.title('猜數字遊戲')
win.geometry('600x600')
win.configure(background ='gray' )

ha = tk.Label(win,text =  '終極密碼',font = ('標楷體',30))
ha.pack()

rankf=tk.Frame(win)
rankf.pack()

rank = tk.Label(rankf,text = '選擇難度 1.簡單(1~10)  2.普通(1~100)  3.困難(1~200) :',font = ('標楷體',15))
rank.pack(side = tk.LEFT)
rank_en = tk.Entry(rankf)
rank_en.pack(side = tk.LEFT)

run = tk. Button(win)
run.pack()

p_button=tk.Button(win,text = '開始',command = play)
p_button.pack()

gus=tk.Frame(win)
gus.pack()

guse = tk.Label(gus,text = '猜的數字:',font = ('標楷體',15))
guse.pack(side = tk.LEFT)
gus_en = tk.Entry(gus)
gus_en.pack(side = tk.LEFT)



r_label = tk.Label(win)
r_label.pack() 


win.mainloop()
貓虎皮 iT邦新手 3 級 ‧ 2022-06-15 16:28:49 檢舉
是因為視窗沒更新所以才當掉喔!
先提供一下,你的遊戲流程是甚麼?
輸入完「難度」跟「猜的數字」後,按下「開始」?
沒猜對再改「猜的數字」後,再按下「開始」,直到猜對?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
貓虎皮
iT邦新手 3 級 ‧ 2022-06-16 07:08:46
最佳解答

必須在while迴圈中持續更新主式窗才不會造成式窗當掉。
更改後的程式碼如下。

import tkinter as tk
import random

def play():
    rankc = int(rank_en.get())
    if rankc == 1 :
        easy(rank1)
    if rankc == 2 :
        normal(rank1)
    if rankc == 3 :
        hard(rank1)

def easy(rank):
    n = random.randint(1,10)
    a = 0
    while True:
        a = a+1
        gue = gus_en.get()
        if gue.isdigit():
            gue = int(gue)
        if gue > n:
            print(gue,"太大了")
        elif gue < n:
            print(gue,"太小了")
        else :
            print("恭喜你猜對了,你猜了",a,'次')
            break
        win.update()

def normal(rank):
    n = random.randint(1,100)
    a = 0
    while True:
        a = a+1
        gue = gus_en.get()
        if gue.isdigit():
            gue = int(gue)
        if gue > n:
            print(gue,"太大了")
        elif gue < n:
            print(gue,"太小了")
        else :
            print("恭喜你猜對了,你猜了",a,'次')
            break
        win.update()

def hard(rank):
    n = random.randint(1,200)
    a = 0
    while True:
        a = a+1
        gue = gus_en.get()
        if gue.isdigit():
            gue = int(gue)
        if gue > n:
            print(gue,"太大了")
        elif gue < n:
            print(gue,"太小了")
        else :
            print("恭喜你猜對了,你猜了",a,'次')
            break
        win.update()

rank1 = []
win = tk.Tk()

win.title('猜數字遊戲')
win.geometry('600x600')
win.configure(background ='gray')

ha = tk.Label(win,text = '終極密碼',font = ('標楷體',30))
ha.pack()

rankf=tk.Frame(win)
rankf.pack()

rank = tk.Label(rankf,text = '選擇難度 1.簡單(1~10) 2.普通(1~100) 3.困難(1~200) :',font = ('標楷體',15))
rank.pack(side = tk.LEFT)
rank_en = tk.Entry(rankf)
rank_en.pack(side = tk.LEFT)

run = tk. Button(win)
run.pack()

p_button=tk.Button(win,text = '開始',command = play)
p_button.pack()

gus=tk.Frame(win)
gus.pack()

guse = tk.Label(gus,text = '猜的數字:',font = ('標楷體',15))
guse.pack(side = tk.LEFT)
gus_en = tk.Entry(gus)
gus_en.pack(side = tk.LEFT)

r_label = tk.Label(win)
r_label.pack()

win.mainloop()

不過你的while會在不斷的增加計數變數a的值,
導致a值遠大於猜測次數。
建議加個按鈕來送出猜測值。

我要發表回答

立即登入回答