Reputation: 457
i'm currently using Visual Studio Code to learn how to use tkinter, i did find some tutorials on youtube to create a mouse event function here. i'm completely a newbie in programming.
here's my code:
from tkinter import *
root = Tk()
def leftClick(event):
print("left")
def middleClick(event):
print("middle")
def rightClick(event):
print("right")
frame = Frame(root, width=300, height=250)
frame.bind("<Button-1>", leftClick)
frame.bind("<Button-2>", middleClick)
frame.bind("<Button-3>", rightClick)
root.mainloop()
i don't know why there's no output when i click somewhere inside the frame. when i click left right or middle button on mouse should return "left" "right" "middle" in output window, i did check the terminal window too and nothing was returned there. check the youtube link above for my reference of what i am doing.
here's the terminal output:
PS C:\Users\abdull\Documents!! Code\single file> cd 'c:\Users\abdull\Documents!! Code\single file'; ${env:PYTHONIOENCODING}='UTF-8'; ${env:PYTHONUNBUFFERED}='1'; & 'C:\Users\abdull\AppData\Local\Programs\Python\Python37-32\python.exe' 'c:\Users\abdull.vscode\extensions\ms-python.python-2018.9.2\pythonFiles\experimental\ptvsd_launcher.py' '59359' 'c:\Users\abdull\Documents!! Code\single file\gtktest.py'
ps: sorry for my bad grammar, (2nd language)
Operating system : Windows 10 mouse = logitech m331
Upvotes: 1
Views: 125
Reputation: 3001
Your frame is not getting placed/rendered in the ui. You need to call the pack
method on it:
from tkinter import *
root = Tk()
def leftClick(event):
print("left")
def middleClick(event):
print("middle")
def rightClick(event):
print("right")
frame = Frame(root, width=300, height=250)
frame.bind("<Button-1>", leftClick)
frame.bind("<Button-2>", middleClick)
frame.bind("<Button-3>", rightClick)
frame.pack() # <---------
root.mainloop()
Upvotes: 3