Detecting mouse clicks in python Tkinter canvas [duplicate] - python

This question already has an answer here:
How to bind a click event to a Canvas in Tkinter? [closed]
(1 answer)
Closed 5 years ago.
Is there a way to detect when the mouse is clicked on a Tkinter canvas in python, only using modules which are downloaded with the python package?

I guess a possible answer would be
root = Tk()
canvas= Canvas(root, width=100, height=100)
canvas.bind("<Key>", key)
canvas.bind("<Button-1>", callback)
canvas.pack()
Where key and callback are functions you have to define yourself.
From How to bind a click event to a Canvas in Tkinter?

Related

Can you make a button using the tkinter canvas? [duplicate]

This question already has answers here:
How to make a Button using the tkinter Canvas widget?
(2 answers)
Closed 2 years ago.
I want to make a button using the Tkinter canvas. But I can't find anything on how to do it. I've tried using the regular button widget, but those don't display nor do anything. If anyone has a way of doing, please tell me!
This works.
canvas.create_rectangle(x1, y1, x1+w, y1+h, fill=fill, tags=tag, width=width)
canvas.tag_bind(tag, "<Button-1>", function)
2nd line is key :)
This generates a button on a canvas, the canvas setting should be changeable without changing the button. Found here https://python-tricks.com/button-in-tkinter/
import tkinter as tk
screen = tk.Tk()
#Screen size
screen.geometry('300x300')
#Button
button_tk = tk.Button(screen, text="Hello")
button_tk.pack(side='top')
screen.mainloop()

How to add a scrollbar to a tkinter window? [duplicate]

This question already has answers here:
Adding a scrollbar to a group of widgets in Tkinter
(3 answers)
Closed 2 years ago.
I don't know how to add a scrollbar to this window. Here's the code:
def winHelp():
winHelp = tk.Tk()
winHelp.geometry("700x700")
winHelp.title("Help")
line1label = tk.Label(winHelp, text="Radioactive Decay Calculator: \n")
There will be some text here that will fill up the window:
line1label.grid(row=0)
line1label.config(justify=LEFT)
If you are talking a Tkinter scrollbar, you should tag Tkinter also. For applying Tkinter scrollbar you should use below code.scroll_bar = Scrollbar(master, option,..)
where master is a parent window.

How to open a new window by clicking a button using Tkinter in python? [duplicate]

This question already has answers here:
How can I open a new window when the user clicks the button?
(2 answers)
Closed 6 years ago.
I want to make a gui application in python for that I was making this type of code.
I have already tired many codes but i was not able to make it up to the requirement.
What's stopping you from doing it, please refer the original post here. But basic code:
import Tkinter as tk
def create_window():
window = tk.Toplevel(root)
root = tk.Tk()
b = tk.Button(root, text="Create new window", command=create_window)
b.pack()
root.mainloop()

Python Tkinter removing disabled button overlay [duplicate]

This question already has an answer here:
tkinter color of disabled buttons / disabled optionmenus
(1 answer)
Closed 8 years ago.
In my Program I use a picture in a button. If I now disable this button with button.configure(state="disabled"), I get a white overlay over the whole button. Can I remove this overlay? If yes how? Thanks in advance. Here is an example code:
import Tkinter as tk
window = tk.Tk()
def disable():
button1.config(state="disabled")
button1=tk.Button(command=disable)
testbild=tk.PhotoImage(file="testbild.gif")
button1.image=testbild
button1.configure(relief="flat", image=testbild, height=180, width=180,
background="lightgreen", activebackground="lightgreen", bd=0)
button1.pack()
window.mainloop()
You may be able to change the color of the stipple by setting a background color at Button creation time:
button1=tk.Button(command=disable, bg='black')

Calling a function based on a Listbox current selection "curselection()" in Tkinter [duplicate]

This question already has answers here:
Getting a callback when a Tkinter Listbox selection is changed?
(3 answers)
Closed 9 years ago.
I have a listbox on a GUI in Tkinter. I would like to implement a routine where if a listbox item is selected a function is called (based on this selection) to modify the gui (add another adjacent listbox). Then if that selection changes, the gui reverts back to its default view. Can this be done? Seems you would need to associate a function to a listbox selection, not sure how to do this or if its possible... Does anyone have the secret?
Its possible to add "select" buttons to the bottom of my listbox, but I wanted to avoid this extra work for user and save space on the GUI.
Thanks to all in advance! Daniel
The listbox will fire the virtual event <<ListboxSelect>> whenever the selection changes. If you bind to it, your function will be called whenever the selection changes, even if it was changed via the keyboard.
Ok nevermind, the link below answers my question with the example below:
http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm
from Tkinter import *
root = Tk()
def callback(event):
print "clicked at", event.x, event.y
frame = Frame(root, width=100, height=100)
frame.bind("<Button-1>", callback)
frame.pack()
root.mainloop()
(replace frame with listbox widget of course)
Works !

Categories

Resources