Source:

from tkinter import Tk, Button, Canvas root = Tk() c = Canvas(root,width=100,height=100) c.pack() def opt(event): print(url1) url1 = "vk.com" url2 = "ok.ru" but1 = Button(text="Vk") but1.bind(sequence="<Button-3>",func=opt) but1.pack() but2 = Button(text="Ok") but2.bind(sequence="<Button-3>",func=opt) but2.pack() root.mainloop() 

How to make it so that when you click on the vk button, the url1 = "vk.com" function is transferred and it writes "vk.com" .

And if you click on ok then the url is transmitted url2 = "ok.ru" and writes "ok.ru"

It is necessary to press "<Button-3>"

  • aside: usually the command parameter is used to hang the action to press the button

1 answer 1

Use lambda

 from tkinter import Tk, Button, Canvas def opt(items): print(items) root = Tk() c = Canvas(root, width=100, height=100) c.pack() url1 = "vk.com" url2 = "ok.ru" but1 = Button(text="Vk") but1.bind(sequence="<Button-3>", func=lambda event: opt(url1)) but1.pack() but2 = Button(text="Ok") but2.bind(sequence="<Button-3>", func=lambda event: opt(url2)) but2.pack() root.mainloop() 
  • Thank you very much both at that time and again!) - oah Iryva