尝试使用Tkinter在字符串中查找字符

时间:2014-12-23 19:37:34

标签: python user-interface if-statement tkinter try-except

我正在为我的学校项目制作一个刽子手游戏并且遇到了障碍。我正在尝试测试我的代码,如果您在GUI上按下的字符是正确的,我是否可以在控制台中输出yay,如果没有则输出boo。我尝试了一下,除了和if if之外,if if说'text'没有定义。 (对不起有关按钮的块,我打算尽快清理它!) 这是代码:

#Hangman

from tkinter import *
import random

root = Tk()


word_list = ["APPLE", "PEAR", "BANNANA"]

word = word_list [random.randrange(0,2)]

#Functions
def click_1 ():
    if text in word == true:
        print ("yay")
    else:
        print ("Boo")



#Frames
hangman_frame = Frame(root).grid(row=0, column=0)
letter_frame = Frame(root).grid(row=1, column=0)

#Buttons
A = Button(letter_frame, text="A", command=click_1).grid(row=0, column=0, sticky=W)
B = Button(letter_frame, text="B", command=click_1).grid(row=0, column=1, sticky=W)
C = Button(letter_frame, text="C", command=click_1).grid(row=0, column=2, sticky=W)
D = Button(letter_frame, text="D", command=click_1).grid(row=0, column=3, sticky=W)
E = Button(letter_frame, text="E", command=click_1).grid(row=0, column=4, sticky=W)
F = Button(letter_frame, text="F", command=click_1).grid(row=0, column=5, sticky=W)
G = Button(letter_frame, text="G", command=click_1).grid(row=0, column=6, sticky=W)
H = Button(letter_frame, text="H", command=click_1).grid(row=0, column=7, sticky=W)
I = Button(letter_frame, text="I", command=click_1).grid(row=0, column=8, sticky=W)
J = Button(letter_frame, text="J", command=click_1).grid(row=0, column=9, sticky=W)
K = Button(letter_frame, text="K", command=click_1).grid(row=0, column=10, sticky=W)
L = Button(letter_frame, text="L", command=click_1).grid(row=0, column=11, sticky=W)
M = Button(letter_frame, text="M", command=click_1).grid(row=0, column=12, sticky=W)
N = Button(letter_frame, text="N", command=click_1).grid(row=1, column=0, sticky=W)
O = Button(letter_frame, text="O", command=click_1).grid(row=1, column=1, sticky=W)
P = Button(letter_frame, text="P", command=click_1).grid(row=1, column=2, sticky=W)
Q = Button(letter_frame, text="Q", command=click_1).grid(row=1, column=3, sticky=W)
R = Button(letter_frame, text="R", command=click_1).grid(row=1, column=4, sticky=W)
S = Button(letter_frame, text="S", command=click_1).grid(row=1, column=5, sticky=W)
T = Button(letter_frame, text="T", command=click_1).grid(row=1, column=6, sticky=W)
U = Button(letter_frame, text="U", command=click_1).grid(row=1, column=7, sticky=W)
V = Button(letter_frame, text="V", command=click_1).grid(row=1, column=8, sticky=W)
W = Button(letter_frame, text="W", command=click_1).grid(row=1, column=9, sticky=W)
X = Button(letter_frame, text="X", command=click_1).grid(row=1, column=10, sticky=W)
Y = Button(letter_frame, text="Y", command=click_1).grid(row=1, column=11, sticky=W)
Z = Button(letter_frame, text="Z", command=click_1).grid(row=1, column=12, sticky=W)

4 个答案:

答案 0 :(得分:2)

我修复了一些语法问题,并在循环中生成按钮。如果这对您有用,请告诉我。

#Hangman

from tkinter import *
import random, functools, string

root = Tk()

word_list = ["APPLE", "PEAR", "BANNANA"]

word = word_list [random.randrange(0,2)]

#Functions
def click_1 (text):
    if text in word:
        print ("yay")
    else:
        print ("Boo")

#Frames
hangman_frame = Frame(root).grid(row=0, column=0)
letter_frame = Frame(root).grid(row=1, column=0)

#Buttons
r = c = 0
for letter in string.ascii_uppercase:
    Button(letter_frame, text=letter, command=functools.partial(click_1, letter)).grid(row=r, column=c, sticky=W)
    c += 1
    if c > 12:
        c = 0
        r += 1

答案 1 :(得分:1)

您可以使用此主题解决您的问题:

How to pass arguments to a Button command in Tkinter?

解决方法是使用按钮的“command”字段调用“click_1”函数时传递text参数,如下所示:

  button = Tk.Button(master=frame, text='A', command= lambda: click_1("A"))

答案 2 :(得分:0)

以这种方式测试布尔值是不好的做法(== true)(同样,你应该大写" True":)

你想尝试更像

的东西
if (text in word):

答案 3 :(得分:0)

我知道你得到了答案,但我只是想把它放在那里。我借了@Fred S'代码并写了这个:

#Hangman
from tkinter import *
import random, functools, string


class HangmanGameFrame(Frame):
    def __init__(self,
                 parent=None,
                 word_list=("APPLE", "PEAR", "BANNANA")):
        super(HangmanGameFrame, self).__init__()
        self.word_list = word_list
        self.word = self.word_list[random.randrange(0,len(word_list))]
        self.create_ui()

    def create_ui(self):
        #Frames
        self.hangman_frame = Frame(root).grid(row=0, column=0)
        self.letter_frame = Frame(root).grid(row=1, column=0)

        def create_buttons():
            #Buttons
            r = c = 0
            for letter in string.ascii_uppercase:
                Button(self.letter_frame, text=letter, command=functools.partial(self.letter_click_handler, letter)).grid(row=r, column=c, sticky=W)
                c += 1
                if c > 12:
                    c = 0
                    r += 1
        create_buttons()            

    def configure(self, *args, **kwargs):
        if kwargs['word_list']:
            self.set_word_list(kwargs['word_list'])
            kwargs.pop('word_list', None)
        super().configure(*args, **kwargs)

    def set_word_list(self, word_list):
        self.word_list = word_list
        self.word = self.word_list[random.randrange(0,len(word_list))]

    def letter_click_handler(self, text):
        if text in self.word:
            print ("yay")
        else:
            print ("Boo")


if __name__ == "__main__":
    root = Tk()
    hangman_frame = HangmanGameFrame(parent=root)
    hangman_frame.configure(word_list=["ZEBRA"]) 
    root.mainloop()

我所做的只是重构代码以使其更清晰。使用面向对象方法IMO完成GUI编程总是更好。它使您的代码更易于扩展和维护。这只是我从原始代码编辑的一些内容。请仔细阅读,看看你是否可以比较并得到一些指示。我相信你的老师会欣赏更好的结构。希望这会有所帮助。

P.S。如果你已经知道这些概念,那么对你有好处,对我来说只是一些练习。欢呼声。