I want to assign a print
function in tkinter Button
, but something goes wrong.
What should I improve?
from tkinter import *
import random
text = random.randint(1, 5)
root = Tk()
root.geometry('550x350')
btn = Button(root, text = 'random', bd = '5'
command = lambda: btn(print(text))
btn.pack(side = 'top')
root.mainloop()
Small error there, btn
is not defined within the lambda
function, you can simply print(text)
there.
btn = Button(root, text = 'random', bd = '5', command = lambda: print(text))
if you want to have a random number on every click you can do the following
btn = Button(root, text = 'random', bd = '5', command = lambda: print(random.randint(1, 5)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.