Listing Option Menu Choices from Text File


 
Thread Tools Search this Thread
Top Forums Programming Listing Option Menu Choices from Text File
# 1  
Old 10-31-2015
Listing Option Menu Choices from Text File

Hello,

I am starting up a tool and one of the initial steps is to select a site/location which is being read from a text file.

Here is the text file contents:

Code:
site1
site2
site3

Here is the code:

Code:
#!/usr/bin/python

from Tkinter import *

root = Tk()
root.geometry("600x400")
root.title("Management E-mail")
# code here

# selecting sites title
title1 = Label (root, text = "What is the site Code?")
title1.pack(fill=X,padx=10)

# selecting sites sites

f = open("C:\Users\name\OneDrive\Python\sitelist.txt", "r")
list = f.read()

var=StringVar ()
var.set ('Click to Select Site')
sitelist = OptionMenu (root, var, *list)
sitelist.pack (fill=X,padx=10)

# input ticket number

title2 = Label (root, text = "What is the ticket number?")
title2.pack (fill=X,padx=10)

text1 = Entry ()
text1.pack(fill=X,padx=10)

listbox = Listbox ()
listbox.pack(side=LEFT,fill=BOTH, expand=1)						  
					  
root.mainloop ()

The problem is the output wherein OptionMenu is reading it per character:

Image

Can you help me list it per site? Thank you in advanced!

Output should be:

site1
site2
site3
# 2  
Old 10-31-2015
When you do list = f.read(), the entire content of file is read into list as a string. And as you know, python iterates over a string variable as if it is an array. And list here is an array of characters and hence your output is all characters one below the other. Here's how you intended to do it:

Code:
f = open("sitelist.txt", "r")
list = []
for line in f:
    list.append(line)
f.close()

And please always remember to close the filehandle after use.
This User Gave Thanks to balajesuri For This Post:
# 3  
Old 10-31-2015
Quote:
Code:
f = open("C:\Users\name\OneDrive\Python\sitelist.txt", "r")
list = f.read()

var=StringVar ()
var.set ('Click to Select Site')
sitelist = OptionMenu (root, var, *list)
sitelist.pack (fill=X,padx=10)

Code:
with open(r'C:\Users\name\OneDrive\Python\sitelist.txt') as f:
    sites = f.readlines()
var=StringVar ()
var.set ('Click to Select Site')
sitelist = OptionMenu (root, var, *sites)
sitelist.pack (fill=X,padx=10)

readlines() loads the file into an array, instead of read() which it loads the file as a string.
list is already used by Python as something else, I would avoid it.
"\n" can cause problems since it represents the end of line, use r'' for raw.
with open() takes care of the maintenance associated with opening a file.

Last edited by Aia; 10-31-2015 at 12:55 PM.. Reason: comment about readlines()
This User Gave Thanks to Aia For This Post:
# 4  
Old 10-31-2015
Quote:
Originally Posted by balajesuri
When you do list = f.read(), the entire content of file is read into list as a string. And as you know, python iterates over a string variable as if it is an array. And list here is an array of characters and hence your output is all characters one below the other. Here's how you intended to do it:

Code:
f = open("sitelist.txt", "r")
list = []
for line in f:
    list.append(line)
f.close()

And please always remember to close the filehandle after use.
Thank you so much! This worked perfectly! Consider this as resolved. Thanks again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Menu option each on a new line

Here is my script for the menu options. # Bash Menu Script Example PS3='Please enter your choice: ' options=("Option 1:" "Option 2:" "Other Reason:" "Quit") select opt in "${options}" do case $opt in "Option 1 :") echo "you chose choice 1" ;; ... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Script to call a menu script and redirect each option to a text file

Hello, I want to design a script that will call an existing menu script and select options one by one and redirict the out put to a file. For example;- In the script MENU.sh there are 10 options i want to design a script MENU2.sh that will select option 2 3 4 6 7 10 and redirict the output... (4 Replies)
Discussion started by: spradha
4 Replies

3. Shell Programming and Scripting

sh file: READ (menu) but now run with option

I have a script which uses READ to detect choice of menu option...now I want to change the script without doing whole rewrite such that when user runs ./script.sh 5 it would execute menu option 5 rather than user running ./script.sh waiting for it to load and then pressing "5 enter" Is it... (1 Reply)
Discussion started by: holyearth
1 Replies

4. Ubuntu

Add Option To Right Menu

hey all, I already installed nautilus-actions now , I want to add "print path" script(option) to the right context menu!.. I did : http://img853.imageshack.us/img853/6973/59818245.png http://img847.imageshack.us/img847/8758/37217230.png the script print located in... (2 Replies)
Discussion started by: eawedat
2 Replies

5. UNIX for Dummies Questions & Answers

Full ps output listing ... what option?

Hi all, Can someone please advise how to get the full listing of a process using the ps command? Or is this not possible and I can only do this only by process ID? Output am wanting to have is similar to when running /usr/ucb/ps -auxwww but preferably using "traditional" ps command that I... (1 Reply)
Discussion started by: newbie_01
1 Replies

6. Shell Programming and Scripting

How do I add the option to change the path in a menu?

How do I add the option to change the path in a menu? I have this script. The user chooses a number and had the option of doing something, looking for log files etc. There is a possibility they might want to look at a different path other than what I have given them such as... (2 Replies)
Discussion started by: taekwondo
2 Replies

7. UNIX for Dummies Questions & Answers

Selectable numeric text menu from file possible?

Is there a way i can take 'lets say the etc/group file' and arange the group names into a selectable numeric menu eg; 1) root:0 2) other:1 3) bin:2 4) sys:3 ..... ..... ..... and so on then GROUP="group name of users selection" i have tried for over an hour searching on... (6 Replies)
Discussion started by: switchkill
6 Replies

8. Shell Programming and Scripting

Unix Shell Script: With Menu Option

I am attempting to create a shell script with the following capaciblities: 1. Listed options to choice from 2. Use to perform awk statements 3. Print a report with the awk results My questions are 1. How do I select more than one file for option #5 and #6 2. How to I create an... (11 Replies)
Discussion started by: jroberson
11 Replies

9. Shell Programming and Scripting

a main menu option?

I have created a main menu in the following way: while true; do echo " " echo "Main Menu: " echo "Please Select An Option Using The Options Provided." echo " " echo "1 - Search All Files" echo " " echo "2 - Search Individual Files" echo " " ... (1 Reply)
Discussion started by: amatuer_lee_3
1 Replies

10. Shell Programming and Scripting

Generating a list of choices in a menu

Hello all, I have the below script and I'm a little stuck on the best way to continue. Essentially I'm creating a text file (systems.txt) with a list of servers in it by hostname. Then I would like to echo a menu with each hostname and a number to use to pick it from the list. It's somehow... (7 Replies)
Discussion started by: sysera
7 Replies
Login or Register to Ask a Question