[Tutor] tkinter message & button questions

Peter Otten __peter__ at web.de
Mon Nov 28 19:52:04 CET 2011


Cranky Frankie wrote:

> I have a program I'm testing in Python 3.1 in Windows XP using tkinker
> to dispay random quotes. It works except for two things.
> 
> 1) The program displays a random quote of the day when it's invoked,
> but I want to have a "Next quote" button so that additional random
> quotes can be displayed. Here are the parts of the program were I get
> the first quote:
> 
> ################################
> def get_random_quote():                            # create a function
> to get a random quote from quote_dict

Crankie, your comments carry very little information, but entirely mess up 
the formatting of the scripts you post. Please avoid all comments but those 
that really help understanding your program. Keep the line length under 80 
chars and start a new line if that doesn't suffice.

> 
>     rq = (random.randrange(len(quote_dict))+1)          # pick a
> random entry from the quote dictionary
>     return rq
> 
> 
> def output_quote():
>     rand_quote = get_random_quote()             # get a random quote
>     quote = quote_dict[rand_quote][1]         # put the quote in a
> string varible
>     author = quote_dict[rand_quote][0]        # put the author in a
> string variable
>     om = (quote+"\n\n"+author)                  # format the output
> string variable
>     return om
> 
> 
> out_message = output_quote()
> 
> msg_widget = Message(win, anchor = NW, justify = LEFT, width = 1000,
> bd= 2, bg = "white",
>                      relief = SOLID, text=out_message)
> 
> ################################
> 
> 
> Here is my button the get the next quote:
> 
> ################################
> 
> next_button = Button(win, text="Next Quote", command=output_quote()) #

Here you are assigning the result of an output_quote() call (i. e. one of 
your quotes) to the command parameter. To get a new quote you need command 
be a function that somehow displays a new quote

def update_quote():
   # your code to make a new quote appear
   # on the screen

next_button = Button(..., command=update_quote)

Note the missing (), you are really passing the function

> create button  widget
> next_button.pack(side=LEFT)
> 
> ################################
> 
> Even though I'm calling the same function, output_quote(), the screen
> is not getting refreshed. There is no error, just the new quote is not
> showing up in the message box. How do I refresh the message box?
> 
> 2) The message widget seems to be sizing based on the size of the text
> you present to it. For a small quote it's small, for a longer quote it
> streches out wide and then adds more lines. I'd like to present a
> consistent sized message box when the program is run, no matter if the
> text is small or large, but I can't figure out how to do that. In
> other words, I'd like the message widget in question 1 above to
> display as a box capable of storing, say, 1,000 characters, even if I
> only give it 100, so the program looks the same every time it's
> invoked. Is there a way to make a default sized message widget?

You may need another layout manager (.place()).




More information about the Tutor mailing list