Tkinter GUI上的一个简单的子手游戏被卡住

时间:2019-06-29 12:42:08

标签: python tkinter

from tkinter import*
import random
wlist ="monday friday sunday blue orange red brown ".split()
sword = random.choice(wlist)    
def lets_begin():       
    guess = ent.get()
    lb2["text"]= ent.get()
    ent.delete(0,5)
    guesses=""    
    turns = 5
    while turns > 0:
        failed = 0
        for char in sword:
            if char in guesses:
                lb1["text"] = char
            else:
                lb1["text"] = "-"
                failed += 1
        if failed==0:
            lb1["text"]="you won"
            break           
        guesses += guess
        if guess not in sword:
            turns -= 1
            lb2["text"]="you are wrong"
        if turns == 0:
            lb2["text"]="game is over.The answer was {0}".format(sword)    
wn=Tk()
wn.geometry("400x300+10+10")
wn.config(bg="silver")    
but1=Button(wn,text="playx",command=lets_begin)
but1.place(x=20,y=95)
ent=Entry(wn,width=12)
ent.place(x=10,y=70)
lb1=Label(bg="pink",width=10)
lb1.place(x=10,y=10)

lb2=Label(bg="yellow",width=28)
lb2.place(x=110,y=5)
lb2["text"]="Days of the week\n and some colors...."+sword
wn.mainloop()

我正在尝试使用Tkinter GUI用Python编写一个简单的“ hang子手”游戏。它适用于控制台IDE。我只是想在TKINTER GUI上进行修改和测试,但是我未能使其运行...如果您能帮助我,我将非常高兴。 lb1 [“ text”]打印的“-”不如“秘密字”所显示的那么多。 每当我输入正确的字符8((

1 个答案:

答案 0 :(得分:2)

我制作了仅使用按钮进行一次检查的版本-没有while循环,该循环会停止mainloop()并且tkinter无法正常工作。将char放入条目后,必须按一下按钮,它会检查此char(和其他变量)并结束工作,因此mainloop()可以再次运行它,并且可以等到下次按下按钮时。

我使用global将值保留在函数之外,因此不会在两次执行之间删除它们。

我一开始没有设置sword和其他值,而是在reset()中设置,所以我可以运行很多次以在下一场游戏之前设置新的sword

我添加一些空格和空行以使代码更具可读性。

import tkinter as tk
import random

wlist = "monday friday sunday blue orange red brown ".split()

def reset():
    '''reset values before first and next game'''
    global turns
    global guesses
    global sword

    turns = 5
    guesses = ""
    sword = random.choice(wlist)    

def check():
    ''' check only once when button is pressed'''
    global turns
    global guesses
    global sword

    # get value from entry
    guess = ent.get()
    guesses += guess

    # check new char in sword to display '-'
    failed = 0
    lb1["text"] = ''

    for char in sword:
        if char in guesses:
            lb1["text"] += char
        else:
            lb1["text"] += "-"
            failed += 1

    # check result
    if failed == 0:
        lb1["text"] = "you won"
        # end of game - set variables before next game
        reset()
        # display after reset because it needs value in `sword`
        lb2["text"] = "Days of the week\n and some colors.... " + sword
    else:
        if guess not in sword:
            turns -= 1
            lb2["text"] = "you are wrong"
        else:
            lb2["text"] = "good choice"

        if turns == 0:
            lb2["text"] = "Game is over.\nThe answer was {0}".format(sword)    
            # end of game - set variables before next game
            reset()
            # display after reset because it needs value in `sword`
            lb2["text"] = "Days of the week\n and some colors.... " + sword

    # clear entry before next char 
    ent.delete(0,'end')

# --- main ---

wn = tk.Tk()
wn.geometry("400x300+10+10")
wn.config(bg="silver")

but1 = tk.Button(wn, text="Check char", command=check)
but1.place(x=20, y=95)

ent = tk.Entry(wn, width=12)
ent.place(x=10, y=70)

lb1 = tk.Label(bg="pink", width=10)
lb1.place(x=10, y=10)

lb2 = tk.Label(bg="yellow", width=28)
lb2.place(x=110, y=5)

# set variables before first game
reset()
# display after reset because it needs value in `sword`
lb2["text"] = "Days of the week\n and some colors.... " + sword


wn.mainloop()