0

Hi,

Below is my test code, i have a list of names in a file, i read the names and i have to insert them as menu options in the menubar. Since its a dynamic menu and people can add new menu's it has to be read from a file everytime its loaded. Problem is that its appending an extra character after everyname in the menu, something line '[]'(check the attached screen shot) which i assume is the newline char, since i'm inserting that too... How can i get rid of that, any suggestions? i tried reading and writing the file in binary mode but makes no difference.

from Tkinter import *

fileObj = open("c:/test","w")
fileObj.write("rajat")
fileObj.write("\n")
fileObj.write("prasun")
fileObj.close()

file1 = open("c:/test","r")
lines = file1.readlines()
file1.close()

root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar)
menubar.add_cascade(label="Names",menu=filemenu)

for line in lines:
    filemenu.add_cascade(label=line)

root.config(menu=menubar)
root.mainloop()
Attachments
2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by Agni
1

Use string method strip() to remove newline characters.

for line in lines:
    filemenu.add_cascade(label=line.strip())
Votes + Comments
thanks a lot..
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.