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

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()

Related

How can I detect if a user click a tkinter button? [duplicate]

This question already has answers here:
How to handle a click button event in python module tkinter
(2 answers)
Closed 2 years ago.
Here is my code!
///
from tkinter import *
import os
import pygame
os.system(‘clear’)
#Window Setup
root = Tk()
root.title(‘cottontail’)
root.geometry(‘800x600’)
frame = Frame(root)
title_screen = Label(root, text = “Choose your rabbits name!”)
title_screen.pack()
name = Text(root, width=10 , height=3)
name.pack()
confirm_name = Button(root, text= “Conirm?”, width = 5, height=3)
root.mainloop()
///
My objective is to take the input the user puts in the text box to make a label in a pygame window with that name. I figured that a button would be an easy way to confirm the name and open the pygame screen. If this makes any sense to you it would really be appreciated if you could help me. Hope you have a good night!
Thanks in advance!
I'm not familiar with tkinter as I'm a PyQt dev, but I can help with your question as the logic behind both of them are same.
You can make a function that is called by the button whenever its clicked, and then, inside that function definition, do whatever you want.
Here's the website that explains this in detail -
https://www.delftstack.com/howto/python-tkinter/how-to-get-the-input-from-tkinter-text-box/

Detecting mouse clicks in python Tkinter canvas [duplicate]

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?

How to create a window with Python [duplicate]

This question already has answers here:
Easiest way to develop simple GUI in Python [closed]
(7 answers)
Closed 6 years ago.
I am learning to program with Python 3.6, and would like to ask for help on building a window. It would be greatly appreciated if someone would please show me the basics, like how to make the window, how to make buttons do things, input and output boxes, and stuff like that. I would prefer not to use pyQT or something like that.
The tkinter module is probably the most common Python GUI method.
To make a button:
from tkinter import *
tk = Tk()
btn = Button(tk, text="a clickable button", command())
btn.pack()
To make an input box:
from tkinter import *
tk = Tk()
inputBox = Entry(tk, bd=5)
#to read your box
inputBox.get()
To make a label:
from tkinter import *
tk = Tk()
label = Label(tk, text="your text here")

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