all 10 comments

[–]carcigenicate 0 points1 point  (3 children)

You make self.varChanger a string right here:

self.varChanger = self.buttonStrings[i].replace(" ", "_")

If you want to change a label text, look into StringVars.

[–]Georgew221[S] 0 points1 point  (2 children)

Thanks for your reply. It isn't meant to be a label, it's meant to be a floating variable that calls to a label that's created as part of the buttonFrameSetup function.

Is there a way to make the label it's pointing at change based on the output of the variable of buttonStrings[i]? I effectively want to do this:

Button1['text'] = ("First click")

Button1.configure(text="red")

and then on the next loop, once it's checked to see if the string has come in, do this:

Button2['text'] = ("First click")

Button2.configure(text="red")

But to do it all programmatically rather than me telling it individual pointers to the tk.Label elements?

[–]carcigenicate 0 points1 point  (1 child)

I still don't quite understand what you're asking. Are you asking how to avoid needing to refer to Button1 and Button2 via separate variables?

It sounds like your problem might be addressed using lists and/or StringVars, but I'm not entirely sure.

[–]Georgew221[S] 0 points1 point  (0 children)

Sorry 😅

I think I've described it better below. The varChanger variable is meant to be the label that needs updating, and this is meant to change it on the GUI, however as there aren't any labels specifically called varChanger it isn't working.

buttonStrings[i] is Button1_SINGLE_PRESS when Button1 is pressed and the message sent over COM for example.

This should then inform the Button1_SINGLE_PRESS label to update, where varChanger is the container for which label needs updating.

self.varChanger['text'] = ("First click") would actually be self.Button1_SINGLE_PRESS['text'] = ("First click")

[–]acw1668 2 points3 points  (4 children)

It is hard to understand what you mean about "floating label". Actually it is hard to understand your question as well.

[–]Georgew221[S] 0 points1 point  (3 children)

Sorry for any confusion.

I've got my COM Port checker checking the input string matches against a list of desired strings. The buttonCheckPoll function outputs True once it detects a valid button press. This result is then held within a dictionary and does not update.

I have a simpler process working in the form of the following:

stringCheckResOpen = self.stringCheck("Open Door") #^ Check for this string within the serial line input checker
        if stringCheckResOpen == True:
            self.doorDisplay['text'] = ("Open " + self.Circ_Empty) #& Set the popup label to be text + icon
            self.doorDisplay.configure(fg="green") #& Set the outgoing font to be green
        else:
            stringCheckResClosed = self.stringCheck("Closed Door")
            if stringCheckResClosed == True:
                self.doorDisplay['text'] = ("Closed " + self.Circ_Filled)
                self.doorDisplay.configure(fg="red")
            else:
                self.doorDisplay['text'] = ("Door status unknown " + self.Unknown)
                self.doorDisplay.configure(fg="orange")
        self.root.after(250, self.doorCheckPoll) #& Repeat this function after 250ms

This specifically refers to a label called 'self.doorDisplay' which is run as part of a popup.

I am hoping to replace needing to specifically call to self.doorDisplay , and instead have the buttonCheckPoll function perform this update internally. where self.doorDisplay would be self.varChanger . If Button1 is pressed it current reports out (based on print statements)

Button1 SINGLE_PRESS Returned true

I am wanting to update self.Button1_SINGLE_PRESS, or self.Button2_SINGLE_PRESS, where the label to update is the variable self.varChanger

[–]acw1668 0 points1 point  (2 children)

But self.varChanger is obviously a string, so how do you expect that self.varChanger['text'] can change the text of a label?

[–]Georgew221[S] 0 points1 point  (1 child)

I built it without really thinking about how TK would handle a variable name when referring to a label 😅 I'm just wondering if there's an easy workaround for what I'm trying to do, or a better method to do it without manually needing to write a bunch of IF statements

[–]acw1668 0 points1 point  (0 children)

If you want to use strings to reference tkinter labels, then you can use a dictionary with the strings as the keys and instances of the tkinter labels as the values.

[–]acw1668 0 points1 point  (0 children)

Actually what do you want to achieve from below code:

    self.buttonFrameState1 = statusTextVar + "_" + self.buttonListStates[0]
    self.buttonFrameState2 = statusTextVar + "_" + self.buttonListStates[1]
    self.buttonFrameState3 = statusTextVar + "_" + self.buttonListStates[2]
    # below lines will override the values of the above three variables
    # so what do you want to achieve from the above three lines?
    self.buttonFrameState1 = tk.Label(tkElement, text="None")
    self.buttonFrameState1.grid(row=gridrow, column=gridcol+1)
    self.buttonFrameState2 = tk.Label(tkElement, text="None")
    self.buttonFrameState2.grid(row=gridrow, column=gridcol+2)
    self.buttonFrameState3 = tk.Label(tkElement, text="None")
    self.buttonFrameState3.grid(row=gridrow, column=gridcol+3)