How To Prevent tkinter Label From Emptying Text Contents

I have created a test snippet to further familiarize myself with the tkinter package/library.
Here is my test code:

import tkinter as tk

class miscTkinter(tk.Tk):
    
    def __init__(self):

        super().__init__()

        # Create main window
        mnWind = tk.Tk
        mnWind.title(self,'Testing Window') # Window title
        mnWind.geometry(self, '290x150')    # Window dimensions

        # Label inside window
        self.label = tk.Label(self, text="Choose One")        
        self.label.place(x=110, y=10)        
     
        # Add buttons
        self.btn1 = tk.Button(self, text="Press 1", command=self.press_one)
        self.btn2 = tk.Button(self, text="Press 2", command=self.press_two)
        
        # Button placements
        self.btn1.place(x=120, y=40)
        self.btn2.place(x=120, y=80)

        # Response to pressing buttons
        self.label1 = tk.Label(self, text="You pressed one!")
        self.label2 = tk.Label(self, text="You pressed two!")

        self.label1_copy = self.label1 
        self.label2_copy = self.label2

        self.button_flag = 3 # other than 1 or 0

    def press_one(self):

        if len(self.label2_copy.cget('text')) != 0 and self.button_flag == 1: # Check if button2 previously pressed
               self.label2_copy.config(text="")    # Clear label1
               print('Entered if statement')

        self.label1_copy = self.label1     
        self.label1_copy.place(x=170, y=42)
        print('Just checking copy: ', self.label1_copy.cget('text'))
        print('Just checking original: ', self.label1.cget('text'))
        self.button_flag = 0
        
    def press_two(self):
        
        if len(self.label1_copy.cget('text')) != 0 and self.button_flag == 0: # Check if button1 previously pressed
            self.label1_copy.config(text="")       # Clear label2
            print('Entered if statement')

        self.label2_copy = self.label2
        self.label2_copy.place(x=170, y=82)
        print('Just checking copy: ', self.label2_copy.cget('text'))
        print('Just checking original: ', self.label2.cget('text')) 
        self.button_flag = 1
            
              
if __name__ == "__main__":
    app = miscTkinter()
    app.mainloop()     

The objective is to display a different message corresponding to the button pressed (they are only different by one word but in a practical scenario, can be completely different messages). I have created two temp variables for the two labels so that the original strings are not deleted as I have to re-display them after clearing the temp string variables. What I notice is that after pressing the button once, the contents of the ‘text’ clears up. I test this via test code where I print the contents both the original and temp variables onto the python editor/shell. How can I prevent the original attributes from clearing the ‘text’ contents?

This does not copy anything. It is a new reference to the same item. See the following:

>>> import tkinter as tk
>>> label1 = tk.Label(text="Original text")
>>> label1.grid()
>>> label1_copy = label1
>>> label1_copy is label1
True

So if you change an attribute of one, it will also change the other.

This is a general rule of Python, unrelated to tkinter.

Hello,

thank you for responding to my python query. I was under the impression of the following:

>>> a=5
>>> b=a
>>> b
    5
>>> b=2
>>> a
    5

As shown in the entered variables, a still equals its original value after changing the value of the intermediary reference variable b.

After these two lines have been executed, a and b refer to the same object, namely 5:

>>> a=5
>>> b=a

Then, after the following line is executed, they refer to different objects:

>>> b=2

Now, a still refers to the object 5, while b refers to the object 2.

Let’s try this:

>>> import tkinter as tk
>>> label_a = tk.Label(text="Label A")
>>> label_b = label_a
>>> label_a is label_b
True
>>> label_b = tk.Label(text="Label B")
>>> label_a is label_b
False

Note that this line assigns a new object to label_b, but does not cause label_a to refer to that new object:

label_b = tk.Label(text="Label B")

Hello,

thank you for clearing up some of the python theory. Much appreciated!
I have gone ahead and rewritten some of my code. It appears to be doing what I wanted it
to do:

import tkinter as tk

class miscTkinter(tk.Tk):
    
    def __init__(self):

        super().__init__()
        
        self.text = {'msg1': 'You pressed one!', 'msg2': 'You pressed two!'}

        # Create main window
        mnWind = tk.Tk
        mnWind.title(self,'Testing Window') # Window title
        mnWind.geometry(self, '290x150')    # Window dimensions

        # Label inside window
        self.label = tk.Label(self, text="Choose One")        
        self.label.place(x=110, y=10)        
     
        # Add buttons
        self.btn1 = tk.Button(self, text="Press 1", command=self.press_one)
        self.btn2 = tk.Button(self, text="Press 2", command=self.press_two)
        
        # Button placements
        self.btn1.place(x=120, y=40)
        self.btn2.place(x=120, y=80)     

        # Response text labels to pressing buttons
        self.label1 = tk.Label(self, text=self.text['msg1'])
        self.label2 = tk.Label(self, text=self.text['msg2'])

    def press_one(self):

        # Clear label 2
        self.label2.config(text="")   

        # Display label 1
        self.label1 = tk.Label(self, text=self.text['msg1'])
        self.label1.place(x=170, y=42)       
        
    def press_two(self):

        # Clear label 1
        self.label1.config(text="")

        # Display label 2
        self.label2 = tk.Label(self, text=self.text['msg2'])
        self.label2.place(x=170, y=84)
            
              
if __name__ == "__main__":
    app = miscTkinter()
    app.mainloop()        
  
    
1 Like