2

I am not able to insert new line in the output that is generated using Label in Tkinter. Below is the image of my GUI that explains my problem.

enter image description here

This is the code that I used for defining my label.

string1 = 'At pathID' + str(l.get('pathId'))+
          'In label' + str(keys) +':'+
          'profile in reference but not in copy' + 
           str(list(set(a_value)-set(b_value)))            

ttk.Label(t.sub_frame, text=string1).pack(side="left", fill="x", expand=1) 

What I want:

I want a new line at the beginning of At path ID0 in label.....

So the output in label should look like below and not like shown in one line above in GUI image:

At path ID0 In label1: profile in reference but not in copy[(18,0,0)] At path ID0 In label2: profile in reference but not in copy[(18,0,8)]

What I tried:

The above shown lines are generated in loop so I tried keeping new line character before start of the line, but it only shows the output from next line yet keeping it attached in one line.

string1 = '\n' + '\nAt pathID' + str(l.get('pathId'))+
              'In label' + str(keys) +':'+
              'profile in reference but not in copy' + 
               str(list(set(a_value)-set(b_value))) 

The reason I assumed was that at every loop new line character will be encountered and I can get every line printed in newline. Can you guys suggest me something? I think I am missing a minor logic somewhere

Radheya
  • 779
  • 1
  • 11
  • 41
  • 2
    Why are you using `/n`? A newline is `\n`. Try this in the interpreter: `print('hello\nworld')` – PM 2Ring Aug 06 '15 at 11:53
  • sorry for the wrong character. In the code I am using correct one. by mistake I used `/n` here. Let me edit question. – Radheya Aug 06 '15 at 11:57

1 Answers1

2

I don't understand why but due to something, the values in pack were causing some problems. I edited my code like shown below. Now its working fine.

ttk.Label(t.sub_frame, text=string1).pack()

Thank you guys for your attention.

Radheya
  • 779
  • 1
  • 11
  • 41
  • 3
    Its recommended to use `pack()` in a separate line on a reference of the object: `label = ttk.Label(t.sub_frame, text=string1); label.pack()`.Maybe your parameters in `pack()` would work this way. If you know the width you can also set `wraplength` of your label: `ttk.Label(parent, text="longtext..." wraplength=20)`, this will break your text automaticly after the given width is reached. Also you should use `format()` for formating your strings, see: [clickme](http://stackoverflow.com/questions/5082452/python-string-formatting-vs-format) – VRage Aug 06 '15 at 12:30
  • This is whats your string would look like: `string1 = 'At pathID {0} In label {1} : profile in reference but not in copy {2}'.format(l.get('pathId'), keys, list(set(a_value)-set(b_value))) ` – VRage Aug 06 '15 at 12:35
  • 1
    This is very helpful. Thank you VRage. – Radheya Aug 06 '15 at 12:47
  • I definitely have to format my strings and in my case I cannot use `wraplength` because the length of string is not fixed. – Radheya Aug 06 '15 at 12:49
  • Maybe in other cases this [text wrapping](https://docs.python.org/2/library/textwrap.html) module comes in handy, so keep that in mind – VRage Aug 07 '15 at 15:27
  • Sure. I'll keep in mind. – Radheya Aug 08 '15 at 17:01