-2

I am quite new to Python, but when I click on one of the buttons, I want the text of the label (the label that has "ctext" in the buttonClicked function) to change based on what formula I want it to be. I tried adding a value to the buttonClicked function, but when I did that it made the "command=buttonClicked(value)" static and didn't happen after I clicked the button.

Here's a portion of my code:

def buttonClicked():

    formulac = tk.Label(root, text='Formula:')

    formula = tk.Label(root, text= ctext) #"Insert formula" must be a string variable

    formulac.config(font=("helvetica", 30))

    canvas1.create_window(1500, 150, window = formulac)

    formula.config(font=("helvetica", 30))

    canvas1.create_window(1500, 200, window=formula)

    entry1 = tk.Entry(root)

    canvas1.create_window(1500, 250, window=entry1)

ctext = "Square"

formula1 = tk.Button(text= "Formula", command= buttonClicked, bg="brown", fg = "white", font =("helvetica", 9))

canvas1.create_window(120, 100, window = formula1)


formula2 = tk.Button(text= "Formula", command= buttonClicked, bg="brown", fg = "white", font =("helvetica", 9))

canvas1.create_window(120, 130, window = formula2)
Matiiss
  • 5,970
  • 2
  • 12
  • 29

1 Answers1

0

"How to change the text of a label using tkinter by clicking a button" You can use label.config(text='something'):

from tkinter import *

root = Tk()

def change():
    label.config(text='changed')

button = Button(root, text='click', command=change)
label = Label(root, text='click button')

button.pack()
label.pack()

root.mainloop()
Tkinter Lover
  • 835
  • 3
  • 19