在Python中将变量设置为GUI标签

时间:2018-06-22 02:48:40

标签: python tkinter

所以我有一个问题,我需要一些帮助修复我的代码。我的目标是让用户在python的GUI的输入框中输入一些特定的文本,然后输出响应(我选择让Chuck Norris和T先生复出)。问题在于它没有输出响应(即恢复)。我要它做的是将响应输出到GUI上。我已经尝试过.set()语法,但是可能做错了。 这是代码:

# IMPORTS #
from tkinter import *
from tkinter import ttk
import random


# CLASS #
class Comeback:
    def __init__(self, comeback_list):
        self.comeback_list = comeback_list

    def get_random_comeback(self):
        return random.choice(self.comeback_list)


def main():
    mr_t_comebacks = ["Every time a church bell rings, Mr. T pities a fool. ",
                      "The last man who made eye contact with Mr. T was Ray Charles. ",

                      "Mr. T is so scary that his hair is actually afraid to grow. The only reason he has a mohawk is because it's in his blind spot. ",
                      "Google doesn't allow you to search for Mr. T because it knows you don't find Mr. T, he finds you. ",
                      "World champion eater Takeru Kobayashi once ate 53.5 hot dogs in 12 minutes. Allotted the same time, Mr. T ate Kobayashi. ",
                      "Children are afraid of the dark. Dark is afraid of Mr. T. ",
                      "Mr. T stole Michael Jackson's black. ", "5 out of 5 doctors recommend not annoying Mr. T. ",
                      "When Mr. T received his star on Hollywood's Walk of Fame, he made his hand prints after the cement was dry. "]
    chuck_norris_comebacks = ["Chuck Norris counted to infinity... Twice.",
                              "When the Bogeyman goes to sleep at night he checks his closet for Chuck Norris.",
                              "Chuck Norris has already been to Mars; that's why there are no signs of life there.",
                              "Guns don't kill people. Chuck Norris kills People.",
                              "Chuck Norris uses pepper spray to spice up his steaks.",
                              "The Great Wall of China was originally created to keep Chuck Norris out. It failed ",
                              "Chuck Norris can win a game of Connect Four in only three moves.",
                              "Chuck Norris can win a game of chess in only one move... a roundhouse kick to the face."]
    mr_t = Comeback(mr_t_comebacks)
    chuck_norris = Comeback(chuck_norris_comebacks)
    answer = who.get().lower()
    if "mr t" in answer:
        print(mr_t)

    elif "chuck norris" in answer:
        print(chuck_norris)

    elif "give up" in answer:
        print("I win")
        exit()
    else:
        print("Put something relevant in or Chuck Norris will team up with Mr. T and they will come for you")
        main()


# GUI CODE #
root = Tk()
root.title("Chuck Norris vs Mr. T")

# Create the top frame
top_frame = ttk.LabelFrame(root, text="Comebacks")
top_frame.grid(row=0, column=0, padx=10, pady=10, sticky="NSEW")

# Create and set the message text variable
message_text = StringVar()
message_text.set("Welcome! It's time for some comebacks!")

# Create and pack the message label
message_label = ttk.Label(top_frame, textvariable=message_text, justify="center")
message_label.grid(row=0, column=1, padx=10, pady=10)

# Create the bottom frame
bottom_frame = ttk.LabelFrame(root, text="User Input")
bottom_frame.grid(row=1, column=0, padx=10, pady=5, sticky="NSEW")

# Create and set the account details variable
question = StringVar()
question.set("Would you like a Mr. T comeback or a Chuck Norris comeback?")

# Create the details label and pack it into the GUI
details_label = ttk.Label(bottom_frame, textvariable=question, wraplength=300, justify="center")
details_label.grid(row=3, column=1, padx=10, pady=3)

# Create a variable to store the response
who = StringVar()
who.set("")

# Create an entry to type in answer
comeback_entry = ttk.Entry(bottom_frame, textvariable=who)
comeback_entry.grid(row=5, column=1, padx=20, pady=5)

# Create a submit button
submit_button = ttk.Button(bottom_frame, text="Submit", command=main)
submit_button.grid(row=7, column=1, padx=100, pady=5)

#  Create the response variable
comeback_text = StringVar()
comeback_text.set("")

# Create the response label and pack it in the GUI
comeback_label = ttk.Label(top_frame, textvariable=comeback_text)
comeback_label.grid(row=2, column=0, columnspan=2, padx=10, pady=10)

# Run the main function
root.mainloop()

任何帮助都会很棒,谢谢

1 个答案:

答案 0 :(得分:0)

else下的main()块中有一个问题,因为它再次调用main()并陷入了循环。在那里取出main()部分。

另一个问题:从未调用Comeback.get_random_comeback()

另一个问题是您从未设置comeback_text

使用此功能块可以实现您的目标:

    mr_t = Comeback(mr_t_comebacks).get_random_comeback()
    chuck_norris = Comeback(chuck_norris_comebacks).get_random_comeback()
    answer = who.get().lower()
    if "mr t" in answer:
        print(mr_t)
        comeback_text.set(mr_t)
    elif "chuck norris" in answer:
        print(chuck_norris)
        comeback_text.set(chuck_norris)
    elif "give up" in answer:
        print("I win")
        exit()
    else:
        print("Put something relevant in or Chuck Norris will team up with Mr. T and they will come for you")