My program won't read the listbox correctly. The output from the print statements are as follows:
Dronfield
Sheffield
2.20
('1',)
I want the list item "B-line" to = 1, but due to the wierdness of the value, it reads it as ('1',)
It is meant to read from the passinfo.txt document, and the businfo.txt document. It works for the latter, but not the former. It is supposed to read the type of pass, and times the ticket price value by the number next to it, so 2.20 would be x0.5 (Halved)
Here are the files in the attachments.
And here's the code:
#Bus Ticket.py
"""A program that generates bus tickets based on data input by the user"""
from Tkinter import *
global passType
global start
global end
global price
class main:
def __init__(self, master):
self.master = root
self.master.title('Bus Tickets')
self.master.geometry('300x250+350+450')
self.labelTitle = Label(self.master, text='Bus Tickets')
self.labelTitle.pack(side=TOP)
self.labelStart = Label(self.master, text='Where are you starting your Journey?')
self.labelStart.pack(side=TOP)
self.entryStart = Entry(self.master)
self.entryStart.pack(side=TOP)
self.labelDest = Label(self.master, text='Where do you want to go?')
self.labelDest.pack(side=TOP)
self.entryDest = Entry(self.master)
self.entryDest.pack(side=TOP)
self.labelList = Label(self.master, text='Choose the bus pass that you use:')
self.labelList.pack(side=TOP)
self.listPass = Listbox(self.master, height=3)
self.listPass.pack(side=TOP)
self.listPass.insert(END,"None")
self.listPass.insert(END,"B-line")
self.listPass.insert(END,"SHU Card")
self.buttonOK = Button(self.master, text="OK", command=self.display)
self.buttonOK.pack(side=BOTTOM)
self.master.mainloop()
def display(self):
global start
global end
global price
global passType
start = self.entryStart.get()
end = self.entryDest.get()
passType=self.listPass.curselection()
fileBus = open("businfo.txt", "r")
for line in fileBus:
variable = line.split(',')
if len(variable) < 3:
continue
if variable[0] == start and variable[1] == end:
price = variable[2]
print variable[0]
print variable[1]
print price
print passType
child()
break
else:
continue
filepass = open("passinfo.txt", "r")
for line in filepass:
variable2 = line.split(',')
if len(variable2) < 3:
continue
if variable2[0] == passType:
print variable2[1]
print variable2[2]
passPrice = price*variable2[2]
print passPrice
class child:
def __init__(self):
global start
global end
global price
self.slave = Toplevel(root)
self.label1 = Label(self.slave, text='Thompson Travel')
self.label1.pack(side=TOP)
self.labelBoard = Label(self.slave, text='Boarding point: '+start)
self.labelBoard.pack(side=TOP)
self.labelDest = Label(self.slave, text='Destination: '+end)
self.labelDest.pack(side=TOP)
self.labelCost = Label(self.slave, text='Price: '+price)
self.labelCost.pack(side=TOP)
root = Tk()
main(root)