0

I'm trying to create a simple paint app in tkinter. I have a function called paint and the code looks like this:

def paint( event):
    
    x1, y1, x2, y2 = ( event.x-5),( event.y-5), ( event.x+5),( event.y+5)
     
   
    Colour = "#CB5E7F"
     
    
    w.create_line( x1, y1, x2,y2, fill = Colour,width=change_width())


    
w = Canvas(root, width = 400, height = 250)
 

w.bind( "<B1-Motion>", paint)
 

l = Label( root, text = "Click and Drag to draw." )
l.place(x=150,y=400)
w.place(x=45,y=80)

I have another function called change_width, to let the user change the width of the line

def change_width():
    options= ["10","20","30"] 



    variable = StringVar(root)
    variable.set(options[0])
    #print(variable) 

    menu= OptionMenu(root, variable, *options)
    #variable.trace_add('write', lambda *args: print(variable.get()))
    menu.configure(bg="black",fg="white",font=('Arial', 14))
    menu.place(x=370,y=10)
    return variable.get()
width=Button(root,text="Change width",bg="black",fg="white",font('Arial',14),command=change_width())
width.place(x=230,y=10)

The problem is that the width doesn't change from the value that I set, and I don't understand why. Normally, it should switch to whichever value I've chosen when I click on OptionMenu.

  • 1
    If I'm reading this correctly, it looks like `variable` will end up being set to `options[0]` every time `change_width()` is called...so it doesn't matter what you select via `menu`. – JRiggles Sep 28 '22 at 19:36
  • You're calling `variable.get()` about a millisecond after creating the variable. The user won't have even seen it by the time it returns. – Bryan Oakley Sep 28 '22 at 19:37
  • Also, I don't know if this is the issue, but you have a typo in `w.create_line()` - you've written `change_with()` instead of `change_width()` – JRiggles Sep 28 '22 at 19:41
  • So you think it's better to create the OptionMenu outside the function? – the futurefeat Sep 28 '22 at 19:53
  • Also, I fixed the typo, but it's still the same problem. – the futurefeat Sep 28 '22 at 19:54

1 Answers1

0

Actually you don't need the Change width button and change_width() function. You just need to create the OptionMenu widget once and get the width using variable.get() inside paint():

from tkinter import *

root = Tk()
root.geometry("600x600")

def paint(event):
    x1, y1, x2, y2 = (event.x-5), (event.y-5), (event.x+5), (event.y+5)
    Colour = "#CB5E7F"
    # get the selected width directly using variable.get()
    w.create_line( x1, y1, x2, y2, fill=Colour, width=variable.get())

w = Canvas(root, width=400, height=250, bg="white")
w.bind("<B1-Motion>", paint)

l = Label(root, text="Click and Drag to draw." )
l.place(x=150, y=400)
w.place(x=45, y=80)

# create option menu for the line width
options= ["10", "20", "30"]
variable = StringVar(root)
variable.set(options[0])

menu= OptionMenu(root, variable, *options)
menu.configure(bg="black", fg="white", font=('Arial', 14))
menu.place(x=370, y=10)

root.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34