Bold an object when clicking it [duplicate] - python

This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 3 years ago.
I want my half circle to be bolded when I click. Currently, it keeps drawing as already bold.
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, width=300, height=200, bg='black')
canvas.pack(fill="both", expand=True)
# when you click on the half circle, it becomes bold
half_circle = canvas.create_arc(100, 0, 200, 100, start=0, extent=-180, outline="white", style="arc")
def bold():
canvas.itemconfigure(half_circle,width=2.5)
canvas.tag_bind(half_circle,"<Button-1>", bold())
root.mainloop()
Update: I changed bold() to bold(event), and am also passing bold. Still not working. I think it could be a problem with PyCharm. Even when I ask it to just print("random") after a click, when the window opens "random" prints immediately and I can't seem to interact with it after.
Second update: I wasn't clicking the exact outline, and now understand the difference between calls and callbacks. lol

The argument should be a callback, not a call:
canvas.tag_bind(half_circle, "<Button-1>", bold)

Related

Set Canvas background as gif Tkinter [duplicate]

This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 11 months ago.
I want to set my Canvas background as gif. Here's what I'm currently trying to do:
def convertPage():
for i in mw.winfo_children():
i.destroy()
drop_file = Canvas(mw, width=270, height=270)
drop_file.place(x=25, y=25)
image = ImageTk.PhotoImage(file="media/ukral.gif")
drop_file.create_image(10, 10, image=image, anchor=NW)
Button(mw, text='KEYWORD IMAGE', command=fileDialog, font=('Inter', 24), fg='#3EC96D', bg='black').place(width=250, height=60, relx=0.1875, rely=0.628)
Button(mw, text='BACK', command=mainPage, font=fnt_sml, fg='#3EC96D', bg='black').place(width=250, height=60, relx=0.1875, rely=0.806)
There are no errors returned in the console, but gif doesn't work, the background of Canvas is white. BTW, I'm doing it from Mac, so maybe this could be the issue.
Update: finally found the solution here:
Play an Animated GIF in python with tkinter
Have you tried to look online?
I found this link, it might be helpful
In short, the answer could be adding the right format kwarg

Tkinter buttons calling functions without pressing them [duplicate]

This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed last year.
I've had this problem for a while. When I start a program, any tkinter buttons will call their functions without being pressed or anything. and after that, I cannot use the button anymore because it only works once for some reason. Can anybody help?
This is not the actual code, but it is an example:
from tkinter import *
root = Tk()
def function():
print("activated")
button = Button(root, text="this is a button", command=function)
button.pack()
root.mainloop()
Apparently adding parentheses at the end of the command inside the button code was the problem. credit to #jasonharper for this one.

Add ScrollBar to a window of labels [duplicate]

This question already has answers here:
Adding a scrollbar to a group of widgets in Tkinter
(3 answers)
Closed 4 years ago.
My code is working well in itself, but doesn't scroll through the Labels (which is what i'm trying to achieve).
I don't want to use canvas or listbox or anything.
import tkinter as tk
master = tk.Tk()
scrollbar = tk.Scrollbar(master).pack(side=tk.RIGHT, fill=tk.Y,command=tk.yview)
label = tk.Label(text="llklkl")
label.place(x=100,y=500)
label2 = tk.Label(text="llklkl")
label2.place(x=1000,y=5000)
tk.mainloop()
Hello and welcome to SO.
The tkinter Scrollbar widget sadly can not be used on a _tkinter.tkapp object, i.e. your main window called master. From effbot.org:
This widget is used to implement scrolled listboxes, canvases, and text fields.
and
The Scrollbar widget is almost always used in conjunction with a Listbox, Canvas, or Text widget. Horizontal scrollbars can also be used with the Entry widget.
That means that you absolutely HAVE to create some widget inside your main window in order to be able to scroll anything, you can`t just scroll the window itself.
That being said, if you wanted to add a Scrollbar to, let's say, a Listbox, that's how you would do it (also taken from the above mentioned website, you should really check it out):
First of all, you have to set the widget’s yscrollcommand callbacks to the set method of the scrollbar.
Secondly, you have to set the scrollbar’s command to the yview method of the widget, much like you did already, but like name_of_object.yview, not tk.yview.
import tkinter as tk
master = tk.Tk()
scrollbar = tk.Scrollbar(master)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
listbox = tk.Listbox(master, yscrollcommand=scrollbar.set)
for i in range(20):
listbox.insert(tk.END, str(i))
listbox.pack(side=tk.LEFT, fill=tk.BOTH)
scrollbar.config(command=listbox.yview)
master.mainloop()
Also, pack the scrollbar in a seperate line. This will produce a window with numbers from 1 to 50 in a scrollable Listbox widget. If i get you right, you want to be able to scroll between your labels? well, i guess you'll have to use some kind of wrapping widget for that, i would recommend a Canvas. But that's really up to you and i'm sure you'll figure it out yourself. If you need any more help, let me know - but please read the docs before asking ;-)

Python Tkinter error with button commands [duplicate]

This question already has answers here:
tkinter creating buttons in for loop passing command arguments
(3 answers)
Closed 5 years ago.
Hi I was trying to make simple calculator using tkinter gui with python.
But first of all I was trying to create buttons that once they are clicked, they were attached to the result shown on the screen. (Just as real-life calculator, if screen shows 12 and I click 3, the screen then shows 123)
from Tkinter import *
class Calculator(Frame):
def __init__(self):
Frame.__init__(self)
self.master.title('Calculator')
self.pack()
self.screen=Frame(self, borderwidth=20, relief=SUNKEN)
self.screen.pack()
self.txtDisplay = Text(self.screen, width=20, height=2)
self.txtDisplay.pack()
self.screen.grid(row=0, columnspan=5) #columnspan to span multiple columns
Contents = ['1','2','3','+','4','5','6','-','7','8','9','*','C','0','=','/']
Buttons = [None]*16
j=1
count=0
for i in range(16):
Buttons[i]=Button(self, text=Contents[i], command=lambda : self.txtDisplay.insert(END, Contents[i]), height=2, width=5, borderwidth=5)
Buttons[i].grid(row=j, column=i%4)
count+=1
if count%4==0:
j+=1
count=0
Calculator().mainloop()
However, the problem is the screen only attaches / whenever I click any button and eventually results in //////////////
/ is last element of Contents list and I guess there is something wrong with
command=lambda : self.txtDisplay.insert(END, Contents[i])
Can I get explanation on why this occurs and how I can deal with it?
Very problem problem with lambda in for loop. You can find many answers.
lambda doesn't use value from i when you create button but when you press button - so all buttons use the same value i which directs to last button.
You have to assign i to argument in lambda and it will copy value from i when you create button
command=lambda x=i: self.txtDisplay.insert(END, Contents[x])

How do I get a button to display a label when clicked using tkinter? [duplicate]

This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 7 years ago.
I want to make a button that displays a label when clicked. Here was my thinking:
from Tkinter import *
def write_hello():
w = Label(root, text = "Hello. This is a label.")
w.pack()
root = Tk()
button = Button(root, text = "Hello!", command = write_hello() )
button.pack()
root.mainloop()
Can anyone be nice enough to walk me through this? I'm very new.
You're calling the write_hello function before you even create the button, so what you're probably seeing is the label showing up before the button on the UI (and without clicking it). What you want to do is pass the function to the Button constructor instead of the function's return value:
button = Button(root, text = "Hello!", command = write_hello)

Categories