Okay , the code that I wrote in the previous post , sucked and it sucked badly. There were many bugs while using it , i was iterating through lists multiple times , when I could have used a simple inbuilt python module. I have revised(rewritten) the code and can be found in this gist .
Now , I have just built a mini – gui for these tables . Its quite easy , and you could use the information to build your own useful GUI apps . I will be explaining each line of code , unlike other times where I usually paste the code 😀 .
First you need to call the main window.
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) self.window.set_size_request(500 , 500) self.window.set_title('Steam Solver') self.window.connect('delete-event' , gtk.main_quit) self.window.modify_bg(gtk.STATE_NORMAL , gtk.gdk.Color(red = 20000, blue = 20000 , green = 20000)) self.TableCreator()
The first three lines are self explanatory , a window is created , the size of the window is set to 500 * 500 , and the window is named Steam Solver . The next line is you connect the event in a window to a function . Here it means that when the window is closed , the gtk loop also exits . Otherwise when you exit the window , the program would still hang .The reason on should use self is because the window or any other thing that you prepend with self can be accessed outside the function throughout the class. The last line is to call the TableCreator function , which is given below.
self.table = gtk.Table(500 ,500 , True) self.label = gtk.Label('Choose any one') self.table.attach(self.label , 25 , 225 , 25 , 60 ) self.box = gtk.combo_box_new_text() self.box.append_text('Pressure') self.box.append_text('Temperature') self.box.connect('changed' , self.PresTemp) self.table.attach(self.box , 275 , 450 , 25 , 60) self.window.add(self.table) self.window.show_all()
You always need a container to hold your widgets. A container can be either a box or a table . I generally prefer tables as they are easier to understand. The table that Ive used is 500 pixels long and 500 pixels wide . True stands for it is homogenous meaning all the pixels are of the same size. gtk.Label creates a label , which is labelled as ‘Choose any one’ . It is then attached to the table . The four numbers that come after self.label are the four co-ordinates , (x1 , y1) , (x2 , y2) of where the label should be attached . gtk.combo_box_new_text creates a combo – box and self.box.append_text , appends two strings to the combo box .(The combo box is one in which an user can choose items from a dropdown list) . You then connect your box to another function , by using the ‘changed’ event . You then add your box to the table and the table to the window.
This is how your window should look.
The next is the PresTemp Function .
index = widget.get_active() widget.destroy() self.Entry1 = gtk.Entry() self.table.attach(self.Entry1 , 275 , 450 , 25 , 60) self.PresTempLab = gtk.Label() self.PresTempEnt = gtk.Entry() self.table.attach(self.PresTempLab , 25 , 225 , 120 , 155) self.table.attach(self.PresTempEnt , 275 , 450 , 120 , 155) self.button = gtk.Button('Process') self.button.connect('clicked' , self.Process , index) self.table.attach(self.button , 200 , 300 , 420 , 480) self.window.show_all()
Widget.get_active gives the index of the item selected in the combo – box. As soon as you call it , it is destroyed and is replaced by an entry widget. gtk.button creates a button named Process and on clicking it , it links to a function Process.The rest of the lines are self explanatory . Well after you change the combo – box the window looks like this.
UserEntry = self.PresTempEnt.get_text() self.PresTempEnt.set_editable(False) widget.destroy() self.SecondStateLab = gtk.Label('Enter Second Property') self.Combo = gtk.combo_box_new_text() for Property in self.PropertyList: self.Combo.append_text(Property) self.Combo.connect('changed' , self.SecState) self.table.attach(self.SecondStateLab , 25 , 225 , 215 , 250) self.table.attach(self.Combo , 275 , 450 , 215 , 250) self.window.show_all()
As soon as the user clicks the process button , the text from the user is stored and non – editable , so that he cannot use it again . Nothing new other than the fact that another combo box has been added. Please notice the fact that the co-ordinates should be chosen so that the widgets dont co-incide . The combo – box has been linked to function which is called SecState which is similar to the PresTemp function , and Im sure you can write it yourself . This is how the window looks after both functions.
The button Process is then linked to the final function , which destroys all the existing tables and combo – boxes , and adds new labels and boxes with the answers.
This is part of the function , which gives only the temperature entry.
self.label.destroy() self.PresTempLab.destroy() self.PresTempEnt.destroy() self.Entry1.destroy() self.PropertyEntry.destroy() self.PropertyLabel.destroy() self.OptionsLabel.destroy() self.SecondStateLab.destroy() self.TemperatureEntry = gtk.Entry() self.TemperatureLabel = gtk.Label('Temperature') self.table.attach(self.TemperatureLabel, 25 , 225 , 25 , 55) self.table.attach(self.TemperatureEntry , 275 , 450 , 25 , 55) widget.connect('clicked' , self.destroy) def destroy(self , widget): self.table.destroy() self.TableCreator()
Nothing much to explain , except that the destroy function destroys the table and brings you back to square one 😀
The entire code can be found here. https://gist.github.com/3771378 .
P . S : Sorry for the really long post . I think my post seems to be inspired by the IMDB top ten movies which go on and on and never seem to end . Anyway thanks for reading.