CaptainProdigy
CaptainProdigy

Reputation: 7

Python: "choice" Function does not seem to be working as I want it to.

Here is the code I'm working with:

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() 

There are no errors, but it keeps printing out all of those texts at the same time, when I'm wanting it to only print out only one of them at random.

My goal is to have someone press the button and out pops a random phrase in the GUI window.

So someone presses the button and only one of any of the four text phrases comes out on the window:

1.Waste not, want not.

2.Sticks and stones

3.Keep holding on.

4.Hold on, Pain Ends.

I believe my troubles are arising from this area right here:

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

Does anyone have any ideas? This is just a very small pet project of mine. I've only been using Python for less than a few months so I'm not very astute. I'm running Python 3.2.5 by the way. Thank you all in advance.

Upvotes: 0

Views: 132

Answers (2)

user2555451
user2555451

Reputation:

How about this:

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()

Instead of just tacking a new message to the bottom of the GUI, this method is cleaner and just updates the text of the message. It also fixes the problem of messages running off of the GUI (you can see this by clicking the button like 30 times).

Upvotes: 0

inspectorG4dget
inspectorG4dget

Reputation: 113945

I originally posted this as a comment, but it turned out to be the answer, so I'm reposting it here:

The problem is that Label(text='Waste not, want not', fg="red").pack() packs the label right away. Doing this with all labels causes them to be packed. It doesn't matter if you call random.choice later because the labels have already been packed into your GUI.

If you want to create a random label from a pool of labels, What you want to do is this:

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()

Upvotes: 1

Related Questions