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

2024-06-25 05:23:13 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有一个问题,我需要一些帮助来修复我的代码。我的目标是让用户在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()

任何帮助都会很好,谢谢


Tags: ofthetotextincreateframelabel
1条回答
网友
1楼 · 发布于 2024-06-25 05:23:13

main()下的else块中有一个问题,因为它再次调用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")

相关问题 更多 >