首页 > 解决方案 > ttk 按钮不执行功能

问题描述

我正在尝试制作一个在用户输入特定情绪时输出圣经经文的应用程序,但我在使用 GUI 时遇到了问题。当我输入一种情绪并按下“提交”按钮时,什么也没有发生,我不知道如何解决它。我在想也许我的 generate_verses 函数有问题?这是我的代码:

from tkinter import *
from tkinter import ttk
import random


def generate_verses(emotion):
    verses_for_sad = ['Psalm 62:6 -- He alone is my Rock and my Salvation, my Refuge; I will not be shaken.',
    'Deuteronomy 31:8 -- And the LORD is the One Who is going ahead of you; He will be with you. He will not desert you or abandon you. Do not fear and do not be dismayed."',
    'Psalm 34:17 -- The righteous cry out, and the LORD hears and rescues them from all their troubles.',
    'Revelation 21:4 -- and He will wipe away every tear from their eyes; and there will no longer be any death; there will no longer be any mourning, or crying, or pain; the first things have passed away."']

    verses_for_happy = ['1 Peter 1:8 -- and though you have not seen Him, you love Him, and though you do not see Him now, but believe in Him, you greatly rejoice with joy inexpressible and full of glory, . . .',
    'Psalm 37:4 -- Delight yourself in the LORD; and He will give you the desires of your heart.',
    'Isaiah 12:2 -- Behold, God is my salvation, I will trust and not be afraid; for the LORD GOD is my Strength and Song, and He has become my salvation."',
    'Psalm 144:15 -- Blessed are the people who are so situated; blessed are the people whose God is the LORD!']

    verses_for_angry = ['Psalm 37:8 -- Cease from anger and abandon wrath; do not get upset; it leads only to evildoing.',
    'Proverbs 14:29 -- One who is slow to anger has great understanding; but one who is quick-tempered exalts foolishness.',
    'Romans 12:19 -- Never take your own revenge, beloved, but leave room for the wrath of God, for it is written: "VENGEANCE IS MINE, I WILL REPAY," says the Lord.',
    'Proverbs 16:32 -- One who is slow to anger is better than the mighty, and one who rules his spirit, than one who captures a city.']

    verses_for_afraid = ['2 Timothy 1:7 -- For God has not given us a spirit of timidity, but of power and love and discipline.',
    '1 John 4:18 -- There is no fear in love, but perfect love drives out fear, because fear involves punishment, and the one who fears is not perfected in love.',
    'Psalm 27:1 -- The LORD is my Light and my Salvation; whom should I fear? The LORD is the defense of my life; whom should I dread?',
    'Deuteronomy 31:6 -- Be strong and courageous, do not be afraid or in dread of them, for the LORD your God is the One Who is going with you. He will not desert you or abandon you."']

    verses_for_grumpy = ['Philippians 2:14-16 -- Do all things without complaining or arguments; so that you will prove yourselves to be blameless and innocent, children of God above reproach in the midst of a crooked and perverse generation, among whom you appear as lights in the world, holding firmly the Word of life, so that on the day of Christ I can take pride because I did not run in vain nor labor in vain.',
    'James 5:9 -- Do not complain, brothers and sisters, against one another, so that you may not be judged; behold, the Judge is standing right at the door.',
    '1 Corinthians 10:9-11 -- Nor are we to put the Lord to the test, as some of them did, and were killed by the snakes. Nor grumble, as some of them did, and were killed by the destroyer. Now these things happened to them as an example, and they were written for our instruction, upon whom the ends of the ages have come.']

    verses_for_anxious = ['''Luke 12:24-26 -- Consider the ravens, that they neither sow nor reap; they have no storeroom nor barn, and yet God feeds them; how much more valuable you are than the birds! And which of you by worrying can add a day to his life's span? Therefore if you cannot do even a very little thing, why do you worry about the other things?''',
    '''Matthew 6:34 -- "So do not worry about tomorrow; for tomorrow will worry about itself. Each day has enough trouble of its own.''',
    '''Romans 8:31 -- What then shall we say to these things? If God is for us, who is against us?''',
    '''Isaiah 35:4 -- Say to those with anxious heart, "Take courage, fear not. Behold, your God will come with vengeance; the recompense of God will come, but He will save you."''']

    if emotion == 'Sad':
        output_verse = random.choice(verses_for_sad)
        return output_verse
    elif emotion == 'Happy':
        output_verse = random.choice(verses_for_happy)
        return output_verse
    elif emotion == 'Angry':
        output_verse = random.choice(verses_for_angry)
        return output_verse
    elif emotion == 'Afraid':
        output_verse = random.choice(verses_for_afraid)
        return output_verse
    elif emotion == 'Grumpy':
        output_verse = random.choice(verses_for_grumpy)
        return output_verse
    elif emotion == 'Anxious':
        output_verse = random.choice(verses_for_anxious)
        return output_verse
    else:
        return 'Please select an emotion from the list (case-sensitive).'


root = Tk()
root.title("Encouragement from the Scriptures")

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

emotion = StringVar()
emotion_entry = ttk.Entry(mainframe, width=7, textvariable=emotion)
emotion_entry.grid(column=2, row=1, sticky=(W, E))

output_verse = StringVar()
ttk.Label(mainframe, textvariable=output_verse).grid(column=2, row=2, sticky=(W, E))

ttk.Button(mainframe, text="Submit", command=lambda: generate_verses(emotion)).grid(column=3, row=3, sticky=W)

ttk.Label(mainframe, text="Are you feeling . . . Sad? Happy? Angry? Afraid? Grumpy? Anxious?").grid(column=3, row=1, sticky=W)

for child in mainframe.winfo_children():
    child.grid_configure(padx=5, pady=5)

emotion_entry.focus()
root.bind("<Return>", lambda: generate_verses(emotion))

root.mainloop()

我只是一个初学者,所以如果代码有点混乱,我很抱歉。

标签: pythontkinterttk

解决方案


您的 generate_verses() 函数返回一个字符串,但实际上并未调用更新 GUI。

您需要有一些东西来处理要在任何地方显示经文的更新。这可以通过多种方式实现,但为简单起见,让我们让 generate_verses() 函数创建一个消息框来显示经文。(https://docs.python.org/3/library/tkinter.messagebox.html

您将需要导入模块:

import tkinter.messagebox as mb

并在 generate_verses() 中创建一个消息框:

output_verse = ''
if emotion == 'Sad':
    output_verse = random.choice(verses_for_sad)
elif emotion == 'Happy':
    output_verse = random.choice(verses_for_happy)
elif emotion == 'Angry':
    output_verse = random.choice(verses_for_angry)
elif emotion == 'Afraid':
    output_verse = random.choice(verses_for_afraid)
elif emotion == 'Grumpy':
    output_verse = random.choice(verses_for_grumpy)
elif emotion == 'Anxious':
    output_verse = random.choice(verses_for_anxious)
else:
    output_verse = 'Please select an emotion from the list (case-sensitive).'
mb.showinfo(title = 'Bible Verses', message = output_verse)

推荐阅读