简体   繁体   中英

How to update a label in Tkinter

How would I go about updating the numpy arrays as labels on my Tkinter application?

Code is following:

from tkinter import *
import tkinter as tk
import numpy as np

# General Tkinter setup
root = Tk()
root.title("Scale Widget")
root.geometry('600x400')
# This is the creation of the sliders
slider1 = Scale(root, from_=0, to=255, resolution=2, orient=HORIZONTAL)
slider1.pack()
slider2 = Scale(root, from_=0, to=255, resolution=2, orient=HORIZONTAL)
slider2.pack()
slider3 = Scale(root, from_=0, to=255, resolution=2, orient=HORIZONTAL)
slider3.pack()
slider4 = Scale(root, from_=0, to=255, resolution=2, orient=HORIZONTAL)
slider4.pack()
slider5 = Scale(root, from_=0, to=255, resolution=2, orient=HORIZONTAL)
slider5.pack()
slider6 = Scale(root, from_=0, to=255, resolution=2, orient=HORIZONTAL)
slider6.pack()
# This is the collation of the NumPy array
numpyvalueupper = np.array([slider1.get(), slider2.get(), slider3.get()])
numpyvaluelower = np.array([slider4.get(), slider5.get(), slider6.get()])
# Help Here please!
label = tk.Label(root, text=numpyvalueupper, bg="blue", fg="white")

print (numpyvalueupper)
print (numpyvaluelower)

root.mainloop()

On creation of a tk label you can input a tk.StringVar and set that text to your numpy array. Here is a little example.

import tkinter as tk
import numpy as np


def updateLabel(e):
    numpyvalueupper = np.array([slider1.get(), slider2.get(), slider3.get()])
    numpyvaluelower = np.array([slider4.get(), slider5.get(), slider6.get()])
    # Just add these together for an example...
    textvar.set(str(numpyvaluelower + numpyvalueupper))

# General Tkinter setup
root = tk.Tk()
root.title("Scale Widget")
root.geometry('600x400')
# This is the creation of the sliders
slider1 = tk.Scale(root, from_=0, to=255, resolution=2, orient=tk.HORIZONTAL, command=updateLabel)
slider1.pack()
slider2 = tk.Scale(root, from_=0, to=255, resolution=2, orient=tk.HORIZONTAL, command=updateLabel)
slider2.pack()
slider3 = tk.Scale(root, from_=0, to=255, resolution=2, orient=tk.HORIZONTAL, command=updateLabel)
slider3.pack()
slider4 = tk.Scale(root, from_=0, to=255, resolution=2, orient=tk.HORIZONTAL, command=updateLabel)
slider4.pack()
slider5 = tk.Scale(root, from_=0, to=255, resolution=2, orient=tk.HORIZONTAL, command=updateLabel)
slider5.pack()
slider6 = tk.Scale(root, from_=0, to=255, resolution=2, orient=tk.HORIZONTAL, command=updateLabel)
slider6.pack()

# Help Here please!
textvar = tk.StringVar()
label = tk.Label(root, textvariable=textvar, bg="blue", fg="white")
label.pack()
textvar.set(str(np.array([slider1.get(), slider2.get(), slider3.get()])))


root.mainloop()

Careful with your imports up top there. You don't need both tkinter imports statements and should just use one format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM