0

the code is

from tkinter import * # Import tkinter

class ChangeLabel:

    def __init__(self):

        window = Tk() # Create a window 
        window.title("Change Label Demo") # Set a title

        # Add a label to frame1
        frame1 = Frame(window) # Create and add a frame to window 
        frame1.pack()        
        self.lbl = Label(frame1, text = "No distance entered yet")
        self.lbl.pack()

        # Add a label, an entry, and a button to frame2
        frame2 = Frame(window) # Create and add a frame to window 
        frame2.pack()
        label = Label(frame2, text = "Enter a distance in kilometres:")
        self.msg = StringVar()
        entry = Entry(frame2, textvariable = self.msg) # Create entry
        btChangeText = Button(frame2, text = "Convert", 
            command = self.processButton) # Button callback method

        label.grid(row = 1, column = 1)
        entry.grid(row = 1, column = 2)
        btChangeText.grid(row = 1, column = 3)

        window.mainloop() # Create an event loop


    def processButton(self):
        calc = float(self.msg.get()) * 0.6214

        self.lbl["text"] = self.msg.get(), "Kilometres is", "%.2f" %calc, "miles"

ChangeLabel() # Create GUI 

in the out put when you put in a number the output says

"20 {kilometres is} 12.43 miles"

how do I remove the {}?

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61

2 Answers2

4

Instead use the str.format method to create a string. What you currently create is a tuple, due to the commas.

self.lbl["text"] = '{} Kilometres is {:.2f} miles'.format(self.msg.get(), calc)

The reason its happening is because thats how the widget renders a tuple. See here for more details.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
2

The 'text' attribute of a label has to be a string, and that the reason for having {} around "kilometres is" because it contains a space in that list of items. You can convert that list of items using " ".join(...), change self.lbl["text"] to:

self.lbl["text"] = " ".join((self.msg.get(), "Kilometres is", "%.2f" %calc, "miles"))
Taku
  • 31,927
  • 11
  • 74
  • 85