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()

Recommended Answers

All 2 Replies

Use string method strip() to remove newline characters.

for line in lines:
    filemenu.add_cascade(label=line.strip())
commented: thanks a lot.. +1

simple and exact ... worked perfectly... Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.