1,105,633 Community Members

Newline "\n" , not working , please help

Member Avatar
arindam31
Light Poster
48 posts since Mar 2011
Reputation Points: -8 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi Guys , this questioned has been asked before , but the answers haven helped me .
I want to seperate lines when i am appending the text control. But "\n" is simply not working .
I am using Python 2.7 , on Windows 7. Please help. I want to display all file names
on next line , whenever user adds a file . Thanks in advance.

Also note , if we create a text ,

>>> hello = "This is a rather long string containing\n\
several lines of text just as you would do in C.\n\
    Note that whitespace at the beginning of the line is\
 significant."
>>> hello
'This is a rather long string containing\nseveral lines of text just as you would do in C.\n    Note that whitespace at the beginning of the line is significant.'

So you can see even this is not working with basic string .

import wx

class TextFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Text Entry Example', size=(500, 450))
        panel = wx.Panel(self, -1)
        menubar=wx.MenuBar()

#Menu section

        pehla=wx.Menu()
        doosra=wx.Menu()  #So there will be two drop down menu

#Menu Bar Items

        menu_1=menubar.Append(pehla,"File")    #Naming of Menu items
        menu_2=menubar.Append(doosra,"Edit")
        self.SetMenuBar(menubar)


#Menu Items
        
        item1_1=pehla.Append(wx.ID_OPEN,"Add","This is add files") #Sub-Items of First menu pull down list
        
        item1_2=pehla.Append(wx.ID_EXIT,"Exit","This will exit app") #The last comment will show on status bar when mouse is on that option

        self.Bind(wx.EVT_MENU, self.OnFileOpen,item1_1)
        #multiLabel = wx.StaticText(panel, -1, "Multi-line")
        self.Bind(wx.EVT_MENU, self.OnFileExit,item1_2)
#Text boxes
        Search_Text = wx.TextCtrl(panel, -1,"Enter search string",pos=(5,5),size=(350,30), style=wx.ALIGN_LEFT)
        Search_Text.SetInsertionPoint(0)

        mytext="\n"
        self.Files_List = wx.TextCtrl(panel, -1,mytext,size=wx.Size(400,100),pos=(5,40),style=wx.TE_READONLY)
        Search_Text.SetInsertionPoint(0)


#Seacrh Button
        
        btn_Process = wx.Button(panel,-1,label='Search',pos=(360,5))
        self.Bind(wx.EVT_BUTTON, self.Process, btn_Process)


#Function Definations

        
    def OnFileOpen(self, e):
        """ File|Open event - Open dialog box. """
        self.dirname = ''
        dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.xls*")
        if (dlg.ShowModal() == wx.ID_OK):
            
            self.fileName = dlg.GetFilename()
            self.dirName = dlg.GetDirectory()
            text1=str(self.fileName)
                       
            self.Files_List.AppendText(text1+"\n")            


            
    def OnFileExit(self, e):
        """ File|Exit event """
        self.Close(True)
    def Process(self,event):
        print"This is a test"




app = wx.PySimpleApp()
frame = TextFrame()
frame.Show()
app.MainLoop()
Member Avatar
arindam31
Light Poster
48 posts since Mar 2011
Reputation Points: -8 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Oops....My mistake.......Didnt make it a multiline.
Just neede to add wx.TE_MULTILINE .

But i still wonder , why the simple string operation did'nt work.
The "hello" text should work.

Member Avatar
~s.o.s~
Failure as a human
10,399 posts since Jun 2006
Reputation Points: 2,496 [?]
Q&As Helped to Solve: 992 [?]
Skill Endorsements: 72 [?]
Administrator
Featured
 
1
 

On the REPL, when you type a name which exists (can be a variable, class name, function etc.), the output shown is the str() representation of that name. For e.g.

>>> s = "Hello\nWorld"
>>> s
'Hello\nWorld'
>>> s.__str__()
'Hello\nWorld'
>>> str(s)
'Hello\nWorld'

If you want to view the output of the interpolated string (after taking into consideration the escape sequences etc.), use the print construct.

>>> s = "Hello\nWorld"
>>> s
'Hello\nWorld'
>>> print s
Hello
World
Member Avatar
arindam31
Light Poster
48 posts since Mar 2011
Reputation Points: -8 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

On the REPL, when you type a name which exists (can be a variable, class name, function etc.), the output shown is the str() representation of that name. For e.g.

>>> s = "Hello\nWorld"
>>> s
'Hello\nWorld'
>>> s.__str__()
'Hello\nWorld'
>>> str(s)
'Hello\nWorld'

If you want to view the output of the interpolated string (after taking into consideration the escape sequences etc.), use the print construct.

>>> s = "Hello\nWorld"
>>> s
'Hello\nWorld'
>>> print s
Hello
World

U r right...It did work when 'Print' is used. But what about places where we cannot use 'Print' . I have to use it in self.Files_List.AppendText(text1+"\n") . Well , i finally used os.linesep() which works well. But its a shame i had to import os just for this purpose . Anyways , thanks , u certainly cleared one concept i did'nt knew.

Question Answered as of 2 Years Ago by ~s.o.s~
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: