Python 巨蟒:“;选择“;函数似乎没有按我希望的那样工作。

Python 巨蟒:“;选择“;函数似乎没有按我希望的那样工作。,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,以下是我正在使用的代码: import sys from tkinter import * from random import choice def motiv(): motiv1 = mLabel = Label(text='Waste not, want not', fg="red").pack() motiv2 = mLabel = Label(text='Sticks and stones', fg="red").pack() motiv3 = mLabel =

以下是我正在使用的代码:

import sys
from tkinter import *
from random import choice
def motiv():
    motiv1 = mLabel = Label(text='Waste not, want not', fg="red").pack()
    motiv2 = mLabel = Label(text='Sticks and stones', fg="red").pack()
    motiv3 = mLabel = Label(text='Keep holding on', fg="red").pack()
    motiv4 = mLabel = Label(text='Hold On, Pain Ends', fg="red").pack()

    ranMotiv = [motiv1, motiv2, motiv3, motiv4]
    print (choice(ranMotiv))

mGui = Tk()

mGui.geometry('450x450+500+150')
mGui.title('RMM')

mLabel = Label(text='Welcome to the Random Motivation Machine', fg="red").pack()

mButton = Button(text = "Click for Some Motivation", command = motiv)
mButton.pack()

mGui.mainloop() 
没有错误,但它会同时打印所有文本,而我希望它只随机打印其中一个

我的目标是让某人按下按钮,然后在GUI窗口中弹出一个随机短语

因此,有人按下按钮,四个文本短语中只有一个出现在窗口上:

1.不浪费,不匮乏

2.棍棒和石头

3.坚持住

坚持住,痛苦结束了

我相信我的麻烦就发生在这里:

ranMotiv = [motiv1, motiv2, motiv3, motiv4]
print (choice(ranMotiv))

有人有什么想法吗?这只是我的一个非常小的宠物项目。我只使用Python不到几个月,所以我不是很精明。顺便说一下,我正在运行Python 3.2.5。提前谢谢大家。

我最初将此作为评论发布,但结果是答案,因此我将其重新发布在这里:

问题是
标签(text='Waste not,want not',fg=“red”).pack()立即打包标签。对所有标签执行此操作会导致它们被打包。稍后调用
random.choice
并不重要,因为标签已经打包到GUI中

如果要从标签池中创建随机标签,请执行以下操作:

def motiv():
    myLabels = ['Waste not, want not', 'Sticks and stones', 'Keep holding on', 'Hold On, Pain Ends']
    chosenLabel = random.choice(myLabels)
    randMotiv = Label(text=chosenLabel, fg="red").pack()
这个怎么样:

from tkinter import *
from random import choice

# Place the messages outside of the function so that they are not
# re-created each time the button is pressed
messages = ['Waste not, want not', 'Sticks and stones',
'Keep holding on', 'Hold On, Pain Ends']

def motiv():

    # Just update the text of the Label instead of create a whole new one
    message["text"] = choice(messages)

mGui = Tk()

mGui.geometry('450x450+500+150')
mGui.title('RMM')

mLabel = Label(text='Welcome to the Random Motivation Machine', fg="red").pack()

mButton = Button(text = "Click for Some Motivation", command = motiv)
mButton.pack()

# Make a Label to hold the messages that will be updated with each click of the button
message = Label(fg="red")
message.pack()

mGui.mainloop()

这种方法不只是将新消息固定到GUI的底部,而是更干净,只更新消息的文本。它还解决了在GUI上运行消息的问题(您可以通过单击按钮30次来查看此问题)。

不要立即打包标签。创建一个文本列表,然后
随机选择。选择其中一个,将其转换为标签和包装,非常感谢您。我要是早点想到那就好了。这肯定很有道理。我刚刚实现了你的建议,它运行得非常好,正是我想要的。再次感谢。您是否打算在创建标签小部件时使用
chosenLabel
?我运行了代码,它可以按照我的要求工作,但我对motiv函数中的message[“text”]=choice(messages)的含义有点困惑。具体来说,信息[“文本”]在做什么/它是什么?对不起,正如我说的,我在这里没有经验。扎查尔:当然可以<代码>消息
是标签实例
message[“text”]
message
的文本选项。您还可以执行
message[“fg”]=“green”
将前景颜色更改为绿色。基本上,Tkinter标签(或任何其他Tkinter小部件)的任何选项都可以这样配置。要查看所有可用选项,可以调用Tkinter小部件的
keys
方法,如下所示:
.keys()