单击提交按钮后,我无法获得所需的输出。它必须显示一张图像和一个标签,以显示他是否赢了,但这似乎不起作用 代码如下:
btn30 = Button(root4, text="SUBMIT",bg="Cyan", fg="Black",width=8,height=1,font=('Helvetica','20'),command=lambda:submit)
btn30.grid(column=11, row=18,pady=5)
用于提交的按钮功能如下:
from tkinter import*
import random
from PIL import ImageTk, Image
from collections import Counter
def emptyblocks(word):
global n
global root2
root2 = Frame(window, relief=RIDGE)
root2.grid(row = 5 , column = 1)
n=len(word)
i=0
global button
button = []
for j in range(n):
btni = Button(root2, text=" ",bg="White", fg="Black",width=3,height=1,font=('Helvetica','20'))
btni.grid(column=i+3, row=4, pady=40)
button.append(btni)
i=i+1
global root3
root3 = Frame(window, relief=RIDGE)
root3.grid(row = 8 , column = 1 )
global root4
root4 = Frame(window, relief=RIDGE)
root4.grid(column=11,row=18)
btn01 = Button(root3, text="Q",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("Q"))
btn01.grid(column=3, row=8,padx=2, pady=2)
btn02 = Button(root3, text="W",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("W"))
btn02.grid(column=4, row=8,padx=2, pady=2)
btn03 = Button(root3, text="E",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("E"))
btn03.grid(column = 5, row=8,padx=2, pady=2)
btn04 = Button(root3, text="R",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("R"))
btn04.grid(column=6, row=8,padx=2, pady=2)
btn05 = Button(root3, text="T",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("T"))
btn05.grid(column=7, row=8,padx=2, pady=2)
btn06 = Button(root3, text="Y",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("Y"))
btn06.grid(column=8, row=8,padx=2, pady=2)
btn07 = Button(root3, text="U",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("U"))
btn07.grid(column=9, row=8,padx=2, pady=2)
btn08 = Button(root3, text="I",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("I"))
btn08.grid(column=10, row=8,padx=2, pady=2)
btn09 = Button(root3, text="O",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("O"))
btn09.grid(column=11, row=8,padx=2, pady=2)
btn10 = Button(root3, text="P",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("P"))
btn10.grid(column=12,row=8,padx=2, pady=2)
btn11= Button(root3, text="A",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("A"))
btn11.grid(column=4, row=9,padx=2, pady=2)
btn12 = Button(root3, text="S",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("S"))
btn12.grid(column=5, row=9,padx=2, pady=2)
btn13 = Button(root3, text="D",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("D"))
btn13.grid(column=6, row=9,padx=2, pady=2)
btn14 = Button(root3, text="F",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("F"))
btn14.grid(column=7, row=9,padx=2, pady=2)
btn15= Button(root3, text="G",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("G"))
btn15.grid(column=8, row=9,padx=2, pady=2)
btn16 = Button(root3, text="H",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("H"))
btn16.grid(column=9, row=9,padx=2, pady=2)
btn17 = Button(root3, text="J",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("J"))
btn17.grid(column=10, row=9,padx=2, pady=2)
btn18 = Button(root3, text="K",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("K"))
btn18.grid(column=11, row=9,padx=2, pady=2)
btn19 = Button(root3, text="L",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("L"))
btn19.grid(column=12, row=9,padx=2, pady=2)
btn20 = Button(root3, text="Z",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("Z"))
btn20.grid(column=5, row=10,padx=2, pady=2)
btn21 = Button(root3, text="X",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("X"))
btn21.grid(column=6, row=10,padx=2, pady=2)
btn22 = Button(root3, text="C",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("C"))
btn22.grid(column=7, row=10,padx=2, pady=2)
btn23 = Button(root3, text="V",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("V"))
btn23.grid(column=8, row=10,padx=2, pady=2)
btn24 = Button(root3, text="B",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("B"))
btn24.grid(column=9, row=10,padx=2, pady=2)
btn25 = Button(root3, text="N",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("N"))
btn25.grid(column=10, row=10,padx=2, pady=2)
btn26 = Button(root3, text="M",bg="skyBlue", fg="Black",width=3,height=1,font=('Helvetica','20'),command=lambda: clicked("M"))
btn26.grid(column=11, row=10,padx=2, pady=2)
btn30 = Button(root4, text="SUBMIT",bg="Cyan", fg="Black",width=8,height=1,font=('Helvetica','20'),command=lambda:submit)
btn30.grid(column=11, row=18,pady=5)
global label3
if word == 'CLOCK':
label3=Label(text="HINT: IT HAS A FACE AND TWO HANDS\n BUT NO ARMS OR LEGS",font="Times 18")
label3.grid(row=2,column=0)
elif word =='BOTTLE':
label3=Label(text="HINT: IT HAS A NECK BUT NO HEAD",font="Times 18")
label3.grid(row=2,column=0)
elif word == 'TOWEL':
label3=Label(text="HINT: IT GETS WETTER AS IT DRIES",font="Times 18")
label3.grid(row=2,column=0)
elif word == 'ELEVEN':
label3=Label(text="HINT: HOW MANY LETTERS ARE THERE IN ALPHABET?",font="Times 18")
label3.grid(row=2,column=0)
elif word == 'AGE':
label3=Label(text="HINT: IT GOES UP BUT NEVER DOWN",font="Times 18")
label3.grid(row=2,column=0)
elif word == 'TABLE':
label3=Label(text="HINT: IT HAS 4 LEGS BUT CAN'T WALK ",font="Times 18")
label3.grid(row=2,column=0)
elif word == 'WINDOW':
label3=Label(text="HINT: THIS INVENTION LETS YOU LOOK\n RIGHT THROUGH THE WALL",font="Times 18")
label3.grid(row=2,column=0)
elif word == 'EGG':
label3=Label(text="HINT: IT HAS TO BE BROKEN BEFORE YOU USE IT",font="Times 18")
label3.grid(row=2,column=0)
elif word == 'SPIDER':
label3=Label(text="HINT: IT BUILD ITS HOUSE WITH EARTHEN STRING\n AND ENSNARES ITS PREY WITH A BITTING STING",font="Times 18")
label3.grid(row=2,column=0)
elif word == 'D':
label3=Label(text="HINT: COMPLETE THE SEQUENCE\n JFMAMJJASON_",font="Times 18")
label3.grid(row=2,column=0)
elif word == 'NEEDLE':
label3=Label(text="HINT: IT HAS ONE EYE BUT CANNOT SEE",font="Times 18")
label3.grid(row=2,column=0)
elif word == 'RELATIONSHIP':
label3=Label(text="HINT: IT IS A SHIP WHICH HAS TWO MATES,\n BUT NO CAPTAIN",font="Times 18")
label3.grid(row=2,column=0)
elif word == 'ENVELOPE':
label3=Label(text="HINT: IT BEGINS WITH 'E' AND ENDS WITH 'E'\n BUT ONLY HAS ONE LETTER",font="Times 18")
label3.grid(row=2,column=0)
elif word == 'EDWIN':
label3=Label(text="HINT: SECOND MAN TO STEP ON MOON",font="Times 18")
label3.grid(row=2,column=0)
elif word == 'DEATH':
label3=Label(text="HINT: SOME TRY TO HIDE,\n SOME TRY TO CHEAT BUT TIME WILL SHOW,\n WE ALWAYS WILL MEET.\n TRY AS YO MIGHT TO GUESS MY NAME ",font="Times 18")
label3.grid(row=2,column=0)
elif word =='SUN':
label3=Label(text="HINT: NEVER RESTING, NEVER STILL, MOVING SILENTLY\n FROM HILL TO HILL,\n IT DOES NOT WALK, RUN OR TROT,\n ALL IS COOL WHERE IT IS NOT ",font="Times 18")
label3.grid(row=2,column=0)
window.mainloop()
def difficulty():
global root1
root1 = Frame(window, relief=RIDGE)
root1.grid(row=4,column=6)
global label1
label1=Label(text="SELECT A DIFFICULTY LEVEL",pady = 30,font = "Times 20")
label1.grid(row=2,column=0,sticky=W)
btn27 = Button(root1, text="EASY",bg="YELLOW", fg="Black",width=8,height=1,bd=10,font=('Helvetica','20'),command=EASY )
btn27.grid(row=4 , column=1,pady=20)
btn28 = Button(root1, text="MEDIUM",bg="Green", fg="Black",width=8,height=1,bd=10,font=('Helvetica','20'),command=MEDIUM)
btn28.grid(row=8 , column=1,pady=20)
btn29 = Button(root1, text="HARD",bg="RED", fg="Black",width=8,height=1,bd=10,font=('Helvetica','20'),command=HARD)
btn29.grid(row=12 , column=1,pady=20)
def EASY():
global chances
chances=8
global words
words=random.choice(easy)
root1.destroy()
label1.destroy()
global label2
label2=Label(text="TOTAL CHANCES : 8",font="Times 18")
label2.grid(row=14,column=0,pady=15)
emptyblocks(words)
def MEDIUM():
global chances
chances=10
global words
words=random.choice(med)
root1.destroy()
label1.destroy()
global label2
label2=Label(text="TOTAL CHANCES : 10",font="Times 18")
label2.grid(row=14,column=0,pady=15)
emptyblocks(words)
def HARD():
global chances
chances=15
global words
words=random.choice(hard)
root1.destroy()
label1.destroy()
global label2
label2=Label(text="TOTAL CHANCES : 15",font="Times 18")
label2.grid(row=14,column=0,pady=15)
emptyblocks(words)
def clicked(alphabet):
global chances
global img
global label4
global label5
global answer
global ans
ans=[]
answer = []
for i in range(n):
c=ord(words[i])
ans.insert(i,c)
j=0
while j<n:
if words[j] == alphabet:
button[j]["text"] = alphabet
d=ord(alphabet)
answer.insert(j,d)
j=j+1
else:
chances=chances-1
txt="CHANCES REMAINING: "+str(chances);
label2.configure(text=txt)
if chances<0:
root2.destroy()
root3.destroy()
root4.destroy()
label2.destroy()
label3.destroy()
load=Image.open('hangman1.png')
render=ImageTk.PhotoImage(load)
img=Label(image=render)
img.image=render
img.grid(row=0,column=15)
label4=Label(text="YOU LOOSE GET READY TO HANG",font="Times 18")
label4.grid(row=10,column=15,pady=20)
def submit():
ans.sort()
answer.sort()
if ans == answer:
root2.destroy()
root3.destroy()
root4.destroy()
label2.destroy()
label3.destroy()
load=Image.open('win.png')
render=ImageTk.PhotoImage(load)
img=Label(image=render)
img.image=render
img.grid(row=0,column=15)
label4=Label(text="CONGRATULATIONS YOU WIN",font="Times 18")
label4.grid(row=10,column=15,pady=20)
window = Tk()
window.title("HANGMAN GAME")
window.geometry("3000x1000")
easy=['CLOCK','BOTTLE','TOWEL','ELEVEN','AGE','WINDOW','TABLE']
med=['EGG','SPIDER','D','NEEDLE']
hard=['RELATIONSHIP','ENVELOPE','EDWIN','DEATH','SUN']
difficulty()
程序的整个代码是:
class CustomAppBarRounded extends StatelessWidget implements PreferredSizeWidget{
final String _appBarTitle;
CustomAppBarRounded(this._appBarTitle);
@override
Widget build(BuildContext context) {
return new Container(
child: new LayoutBuilder(builder: (context, constraint) {
final width = constraint.maxWidth * 8;
return new ClipRect(
child: Stack(
children: <Widget>[
new OverflowBox(
maxHeight: double.infinity,
maxWidth: double.infinity,
child: new SizedBox(
width: width,
height: width,
child: new Padding(
padding: new EdgeInsets.only(
bottom: width / 2 - preferredSize.height / 3
),
child: new DecoratedBox(
decoration: new BoxDecoration(
color: Colors.indigo,
shape: BoxShape.circle,
boxShadow: [
new BoxShadow(color: Colors.black54, blurRadius: 10.0)
],
),
),
),
),
),
new Center(
child: new Text("${this._appBarTitle}",
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.white,
shadows: [
Shadow(color: Colors.black54, blurRadius: 10.0)
]
),
)
),
],
)
);
}),
);
}
@override
Size get preferredSize => const Size.fromHeight(100.0);
}
代码首先执行,询问要选择的难度级别,然后在接下来的情况下告诉您在隐藏的单词中输入字母,必须在给定的机会下进行猜测。如果机会小于零,则游戏结束,但是当我输入正确的字母并尝试提交字母时,submit函数将不会执行。 谁能帮我这个。 谢谢!!!
答案 0 :(得分:0)
我不太确定这是否可以解决问题,我会发表评论,但是我的声誉不高。
您可以尝试摆脱lambda函数,并且可以使用函数名,因为不需要参数。
btn30 = Button(root4, text="SUBMIT",bg="Cyan", fg="Black",width=8,height=1,font=('Helvetica','20'),command=submit)
btn30.grid(column=11, row=18,pady=5)
不太确定这是否可行,但值得一试。 抱歉,如果这不起作用,但我无法发表评论。
祝你好运!
答案 1 :(得分:0)
列表ans
和answer
存在一些问题。一件事是您为功能answer
中的每一次按键按下创建一个空白列表clicked()
。这意味着,您只会在answer
中单击最后一个字母。
此外,可变的数据类型在设置为全局时的行为也有所不同,例如。列表ans
和answer
。您不需要它们是全局的即可更改其内容。我发现最简单的方法是在全局名称空间中声明它们,然后在函数内部对其进行更改。无需让它们具有全局内部函数。
我也提取了很多代码以使其更易于调试:
from tkinter import*
import random
from PIL import ImageTk, Image
def emptyblocks(word):
global n
global root2
root2 = Frame(window, relief=RIDGE)
root2.grid(row=5 , column=1)
n = len(word)
i = 0
# Setting ans only once
for i in range(n):
c = ord(words[i])
ans.insert(i,c)
global button
button = []
for j in range(n):
btni = Button(root2, text=" ", bg="White", fg="Black",
width=3, height=1, font=('Helvetica','20'))
btni.grid(column=i+3, row=4, pady=40)
button.append(btni)
i=i+1
global root3
root3 = Frame(window, relief=RIDGE)
root3.grid(row = 8 , column = 1 )
global root4
root4 = Frame(window, relief=RIDGE)
root4.grid(column=11,row=18)
for row, keys in enumerate(['QWERTYUIOP', 'ASDFGHJKL', 'ZXCVBNM']):
for col, char in enumerate(keys):
btn = Button(root3, text=char, bg="skyBlue", fg="Black",
width=3, height=1, font=('Helvetica','20'),
command=lambda c=char: clicked(c))
btn.grid(column=col+3+row, row=8+row, padx=2, pady=2)
btn30 = Button(root4, text="SUBMIT", bg="Cyan", fg="Black",
width=8, height=1, font=('Helvetica','20'),
command=submit)
btn30.grid(column=11, row=18, pady=5)
global label3
if word == 'CLOCK':
label3=Label(text="HINT: IT HAS A FACE AND TWO HANDS\n BUT NO ARMS OR LEGS", font="Times 18")
label3.grid(row=2,column=0)
elif word =='BOTTLE':
label3 = Label(text="HINT: IT HAS A NECK BUT NO HEAD", font="Times 18")
label3.grid(row=2,column=0)
window.mainloop()
def difficulty():
global root1
root1 = Frame(window, relief=RIDGE)
root1.grid(row=4,column=6)
global label1
label1 = Label(text="SELECT A DIFFICULTY LEVEL",pady = 30,font = "Times 20")
label1.grid(row=2,column=0,sticky=W)
btn27 = Button(root1, text="EASY", bg="YELLOW", fg="Black",
width=8, height=1, bd=10, font=('Helvetica','20'),
command=EASY )
btn27.grid(row=4 , column=1,pady=20)
def EASY():
global chances
chances = 8
global words
words = random.choice(easy)
root1.destroy()
label1.destroy()
global label2
label2 = Label(text="TOTAL CHANCES : 8", font="Times 18")
label2.grid(row=14, column=0, pady=15)
emptyblocks(words)
def clicked(alphabet):
global chances
global img
global label4
global label5
# Do not reset ans for every clicked letter
j = 0
while j < n:
if words[j] == alphabet:
button[j]["text"] = alphabet
d = ord(alphabet)
answer.insert(j, d)
j = j + 1
else:
chances = chances - 1
txt = "CHANCES REMAINING: " + str(chances);
label2.configure(text=txt)
if chances < 0:
root2.destroy()
root3.destroy()
root4.destroy()
label2.destroy()
label3.destroy()
load = Image.open('hangman1.png')
render = ImageTk.PhotoImage(load)
img = Label(image=render)
img.image = render
img.grid(row=0, column=15)
label4 = Label(text="YOU LOOSE GET READY TO HANG", font="Times 18")
label4.grid(row=10, column=15, pady=20)
def submit():
# You already have the answer: words
# Compute the guess from buttons
guess = ''.join([b['text'] for b in button])
if guess == words:
root2.destroy()
root3.destroy()
root4.destroy()
label2.destroy()
label3.destroy()
load=Image.open('win.png')
render=ImageTk.PhotoImage(load)
img=Label(image=render)
img.image=render
img.grid(row=0,column=15)
label4=Label(text="CONGRATULATIONS YOU WIN",font="Times 18")
label4.grid(row=10,column=15,pady=20)
answer = []
ans = []
window = Tk()
easy=['CLOCK','BOTTLE']
difficulty()
仍然存在问题。例如,当单词包含一个字母时,尝试按两次该字母。它会在answer
中出现两次,并且不会与ans
进行匹配。
更新示例
我不十分清楚您在检查答案时的推理方式,但是这里建议您如何做:
您已经具有要查找的字符串:words
。然后从键盘上方的按钮行(button
)中读取所有文本。然后比较两者。我已经相应更新了示例中的代码。