使Python GUI调用特定函数

时间:2018-06-19 00:50:06

标签: python tkinter

所以我在使用这个代码时遇到了麻烦。我想要它做的是用户输入"真实"或"假的"然后他们点击提交按钮,程序输出这句话"你的随机动物是..." (它会有一个随机的动物点)。我无法工作的是get_animal函数。那么它的作用是将响应输出给用户。但我无法使用我的GUI。

这是我的代码:

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

###### FUNCTIONS AND SETUP ######
def get_animal(lit):
    rand_animal = random.choice(lit)
    print("Your random animal is {}".format(rand_animal))


def real_or_fake(user_input):
    user_input.strip()
    if user_input == "Real" or user_input == "Fake":
        return True
    else:
        return False


def main():
    answer = animal.get().capitalize()
    if real_or_fake(answer) == True:
        if answer == "Real":
            response_text.set(get_animal(real_animals))
            play_again = input("Would you like to play again? (Y or N) ").capitalize()
            if play_again == "Y":
                main()
            else:
                exit()

        else:
            response_text.set(get_animal(fake_animals))
            play_again = input("Would you like to play again? (Y or N) ").capitalize()
            if play_again == "Y":
                main()
            else:
                exit()
    else:
        main()

# Set up lists
real_animals = ["horse", "gyrfalcon", "bull", "lion", "meerkat", "cat", "mouse", "moose", "rat", "panda", "stone fish", "anaconda", "python", "owl", "pigeon", "cow", "rabbit", "dog", "narwhal", "killer whale", "shrimp", "plankton", "liger"]
fake_animals = ["unicorn", "phoenix", "minotaur", "orge", "goblin", "hobgoblin", "dragon", "griffin", "centaur", "mermaid", "leviathan", "pegasus", "harpies", "gorgon", "hydra", "sphinx", "raiju", "godzilla", "kraken", "gremlins", "simba", "yogi bear", "bugs bunny", "bambi"]


##### GUI CODE #####
root = Tk()
root.title("Real or Fake")

# Create the top frame
top_frame = ttk.LabelFrame(root, text="Random Animals")
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! You can choose either a real animal or a fake animal.")

# Create and pack the message label
message_label = ttk.Label(top_frame, textvariable=message_text, wraplength=500)
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
animal_details = StringVar()
animal_details.set("Animal: \nWe're going to give you an animal. Would you like Real or Fake? ")

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

# Create a variable to store the amount
animal = StringVar()
animal.set("")

# Create an entry to type in amount
animal_entry = ttk.Entry(bottom_frame, textvariable=animal)
animal_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
response_text = StringVar()

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


#Run the main function
root.mainloop()

如果您对如何使其工作有任何建议,那将非常感谢。

1 个答案:

答案 0 :(得分:0)

您需要返回值,而不是打印它。像这样:

def get_animal(lit):
    rand_animal = random.choice(lit)
    return "Your random animal is {}".format(rand_animal)