[Tutor] Shortening the code of a finsihed program.
Mic
o0MB0o at hotmail.se
Thu Nov 24 17:20:48 CET 2011
Good afternoon!
I have previously asked questions about how I can shorten my code.
I have recieved a lot of good answers, but there is one thing that never
works.
I can never implement the suggestions into my code, and that is my fault. I
have in my
previous questions only asked how to shorten certain parts of a larger
program. And while
the suggestions have managed to solve these problems. I can't never get it
to work when
in my real program.
So I figured that instead of asking questions about how I can shorten
certain parts of my programs,
and then try to put these parts togheter into a large program, I decided to
post my entire program.
It is already fullfilling its purpose, but I want to make the code a lot
shorter.
The program is supposed to be an online ticket booking system.
To book a chair, click on a green chair. It then turns red, which means that
you have booked the chair.
if you wish to unbook your chair, press the red button once. It will then
turn green, which means that you have unbooked
your ticket, and the chair is now bookable again. If the button is red, a
file is being created with information about the chair.
If the buttons turns green, the file is removed, which means that your
ticket is unbooked.
Hopefully, you will get a better overlook of what I am trying to achieve. I
apologize if my English is flawed, but I hope it is understandable.
Also, I have translated the entire program into English from my own languge.
Thanks a lot your help and time!
#Program translated into English.
#Importing necessary modules.
from tkinter import*
import os
chair1_color="green"
chair1_value=False
chair2_color="green"
chair2_value=False
#Creates the Main Menu Window.
class Main_Menu_Window(Frame):
def __init__(self,master):
super(Main_Menu_Window,self).__init__(master)
self.grid()
self.create_mainmenu_widgets()
#Creates the widgets in the window.
def create_mainmenu_widgets(self):
self.instructions=Label(self, text="Welcome to our online booking
system")
self.instructions.grid()
self.to_booking_window=Button(self, text="Book/Unbook your ticket",
command=self.book_unbookmode)
self.to_booking_window.grid()
self.exit_program=Button(self, text="Exit Program",
command=self.exit_program)
self.exit_program.grid()
#Creates a method that quits the program if the user presses the button
"Exit Program"
def exit_program(self):
root.destroy()
#Creates a new window where you book your tickets if you press the
button "Book/Unbook your ticket".
def book_unbookmode(self):
class BookOrUnbookTicketWindow(Frame):
def __init__(self,master):
super(BookOrUnbookTicketWindow,self).__init__(master)
self.grid()
self.create_book_unbookmode_widgets()
#Creates widgets to place in the BookOrUnBookTicketWindow.
def create_book_unbookmode_widgets(self):
self.instructions_bookticket=Label(self, text="""To book a
chair, click on a green chair. It then turns red, which means that you have
booked the chair.
if you wish to unbook
your chair, press the red button once. It will then turn green, which means
that you have unbooked
your ticket, and the
chair is now bookable again.""")
self.instructions_bookticket.grid()
#Creates two chairs. It should be more, but this is just used as
an example. To get more chairs, this code in the section below
#this comment will need to be shortened somehow, otherwise the
code would be way too long.
self.chair1=Button(self, bg=chair1_color, text="01",
command=self.chair1_clicked)
self.chair1.grid()
self.chair2=Button(self, bg=chair2_color, text="02",
command=self.chair2_clicked)
self.chair2.grid()
def chair1_clicked(self):
""" This method runs if chair one is clicked"""
def change_chair1_value():
global chair1_value
chair1_value=not chair1_value
change_chair1_value()
if chair1_value:
self.chair1.configure(bg="Red")
text_file=open("New_York_Canada_15_00_Chair1","w")
text_file.write( "New York-Canada\nSeat:1")#Notice that
I want the Seat number written into the
#textfile
should be 1, if you press chair one,
#2 if you
press chair 2, 3 if you press chair 3 and so on.
#However,
the text should be "New York-Canada should
# be the
same for all chairs.
text_file.close()
def change_chair1_color_red():
global chair1_color
chair1_color=("red")
change_chair1_color_red()
else:
self.chair1.configure(bg="green")
os.remove ("New_York_Canada_15_00_Chair1")
#The file is supposed to be removed because
#it means that you have unbooked your ticket.
def change_chair1_color_green():
global chair1_color
chair1_color=("green")
change_chair1_color_green()
#-------------------------------------------------
def chair2_clicked(self):
""" Ths method runs if chair two is clicked"""
def change_chair2_value():
global chair2_value
chair2_value=not chair2_value
change_chair2_value()
if chair2_value:
self.chair2.configure(bg="Red")
text_file=open("New_York_Canada_15_00_Chair2","w")#The
file name should end with 1 if chair one is pressed.
#if
chair2 is pressed, it should end with chair2. If chair three is
#pressed
it should end with chair3, and so on. The start of the filname
#"New_York_Canada_15_00_
should be the same for all chairs.
text_file.write( "New York-Canada\nSeat:2")#Notice that
I want the Seat number written into the
#textfile
should be 1, if you press chair one,
#2 if you
press chair 2, 3 if you press chair 3 and so on.
#However,
the text should be "New York-Canada should
# be the
same for all chairs.
text_file.close()
def change_chair2_color_red():
global chair2_color
chair2_color=("red")
change_chair2_color_red()
else:
self.chair2.configure(bg="green")
os.remove ("New_York_Canada_15_00_Chair2")
#The file is supposed to be removed because
#it means that you have unbooked your ticket.
def change_chair2_color_green():
global chair2_color
chair2_color=("green")
change_chair2_color_green
#-------------------------------------------------
#As seen above, the two functions are very similiar. I have no
#idea how to make this code shorter and more elegant. The
program
#is already fullfilling its purpose. But instead of writing a
function
#for each chair I want to solve it somehow.
root=Tk()
root.title("Book/Unbooking Mode")
root.geometry("800x800")
app=BookOrUnbookTicketWindow(root)
root.mainloop()
root=Tk()
root.title("Online Booking System")
root.geometry("200x200")
app=Main_Menu_Window(root)
root.mainloop()
More information about the Tutor
mailing list