0

I want to print a n_clusters_ variable in a label, but it's not updating. The computation of n_clusters_ is in another class. But I already set it as global variable. This is the code:

class nCluster():
    def __init__(self):
        label = Label(LabelHasil, text="Jumlah cluster yang ditemukan : ")
        label.pack(side=LEFT, padx=5, pady=5)
    def n_cluster(self):
        cluster_label = Label(LabelHasil, textvariable=n_clusters_)
        cluster_label.pack(side=LEFT, padx=5, pady=5)
show_n_cluster = nCluster()

This is the class where n_clusters_ is computed:

class Proses:
    def __init__(self):
        button = Button(window, text="Proses", command=lambda: stdbscan(self), width=10)
        button.pack()
    def stdbscan(self):
         ...
        global n_clusters_
        n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0) 
show_proses = Proses()

I need to print n_clusters_ variable whenever it's updated. Is it possible?

1 Answer 1

1

you define nCluster.n_cluster which should create and pack your label, but never call it. This doesn't actually need to be in its own method, either, just toss it in at the end of nCluster.__init__.

The big problem is you need to make n_clusters_ a tkinter.StringVar so that it updates properly.

class Proses:
     ...
    def stdbscan(self):
     ...
    global n_clusters_
    n_clusters_ = StringVar()
    n_clusters_.set( len(set(labels)) - (1 if -1 in labels else 0) )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.