1

How can I print a variable in GUI label?

  1. Should I make it a global variable and place it in textvariable=a ?

  2. Could I redirect it somehow to textvariable?

  3. Is there any other way?

#Simple program to randomly choose a name from a list

from Tkinter import * #imports Tkinter module
import random  #imports random module for choice function
import time #imports time module

l=["Thomas", "Philip", "John", "Adam", "Kate", "Sophie", "Anna"]       #creates a list with 7 items
def pri():  #function that asigns randomly item from l list to     variable a and prints it in CLI
    a = random.choice(l) #HOW CAN I PRINT a variable IN label     textvarible????
    print a     #prints in CLI
    time.sleep(0.5)


root = Tk()                 #this
frame = Frame(root)         #creates
frame.pack()                #GUI frame
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )

#GUI button
redbutton = Button(frame, text="Choose name", fg="red", command = pri)
redbutton.pack( side = LEFT)

#GUI label
var = StringVar()
label = Label(bottomframe, textvariable= var, relief=RAISED )
label.pack( side = BOTTOM)

root.mainloop() 

1 Answer 1

1
from Tkinter import *
import time
import random

l=["Thomas", "Philip", "John", "Adam", "Kate", "Sophie", "Anna"]       #creates a list with 7 items
def pri():  #function that asigns randomly item from l list to     variable a and prints it in CLI
    a = random.choice(l) #HOW CAN I PRINT a variable IN label     textvarible????
    print a     #prints in CLI
    time.sleep(0.5)
    return a


root = Tk()

var = IntVar() # instantiate the IntVar variable class
var.set("Philip")     # set it to 0 as the initial value

# the button command is a lambda expression that calls the set method on the var,
# with the var value (var.get) increased by 1 as the argument
Button(root, text="Choose name", command=lambda: var.set(pri())).pack()

# the label's textvariable is set to the variable class instance
Label(root, textvariable=var).pack()

mainloop()
Sign up to request clarification or add additional context in comments.

9 Comments

If you have achieved what you wanted can you please mark this question as answered.
Thank you for your help. My question is how can I print a random name from the l list in GUI label instead of CLI. It dosen’t work if I write: var.set(a) or global a. Any ideas?
@bodgerandscarper -Have edited my answer. It prints randomly a name from your list
It works at first but when I press the button it gives error: >>> Philip Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in call return self.func(*args) TypeError: pri() takes exactly 1 argument (0 given) >>>
Can you please elaborate what error are you getting? I am able to get the output without any issues
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.