15
Tkinter: example of use ## CONTENTS Short introduction Brief explanation of the code Final Code ### --- Short introduction ---#### This is a very simple use of Tkinter in Python 2.7. First of all this was done with: * Python 2.7 * Python Scripter 2.5.3.0 x86 The code was make it in Windows XP Service Pack 3, so if there is some bug please contact with the author at : [email protected], I would appreciate some politeness always.

Tkinter: example of use

Embed Size (px)

DESCRIPTION

A simple code that use Tkinter to make a simple calculator of the Reynolds number.

Citation preview

Page 1: Tkinter: example of use

Tkinter: example of use

## CONTENTSShort introductionBrief explanation of the codeFinal Code

### --- Short introduction ---####This is a very simple use of Tkinter in Python 2.7.

First of all this was done with:* Python 2.7* Python Scripter 2.5.3.0 x86

The code was make it in Windows XP Service Pack 3, so if there is some bug please contact with the author at : [email protected], I would appreciate some politeness always.

This document was done just with the solely purpose of showing some of the ways in which can be used Tkinter, the standard Python interface to the "Tk" graphical user interface (GUI) toolkit.

It's expected that the reader most be familiar with the basis of Python.(Lots of information about it in the internet and in its official page www.python.org/)

Page 2: Tkinter: example of use

###--- The code -----#####(You can copy and paste the code that it’s in final page of this document :P)

Page 3: Tkinter: example of use

###--- Brief explanation of the code -----#####The purpose is to make a calculator of the Reynolds Number in a circular pipe with SI units.

The code will start calling all from Tkinter

And also there must be the declaration of the function Tk() which goes like :

This could have been declared in the line 3 but I considered better to put it after the definition of the function calc_Re()Then it's defined the function that will calculate the Reynolds number with the form

Re = velocity * diameter / kinematic_viscosity

Page 4: Tkinter: example of use

Well, in this case we use just one style of the ways in which is calculated the Reynolds number.In the line number 4 we start declaring the function without any argument at all.

But then we have a division, in this case is the value of nu, the kinematic viscosity, so we must make some exception in order to not get the zero division error, so I decided to just show a message that says “Nu = 0”, so that could be changed to other stuff, like a pop up window that shows the alert or something like that, so that is up to you.Anyway, all comes to a simple conditional, and the “else” part will be the part in which the operations are made it.The line 15 is what will be show in the python shell or command window or whatever-that-the-black-window-is-named.The line 16 will show the result in the window that will be created; the Label function is in charge of that. In the arguments are required “root”, that is declared in the line 19, and the “text” that will be shown in that area of the window.

Page 5: Tkinter: example of use

When it comes to the Label function, the first argument would be for the main window in which is shown the graphical interface, and then comes the second argument which in this case we named text and will be a string, so the numeric values must be changed to string with the function str().The other thing is regarding to the grid issue, which must be take it as a table of excel (or something akin to that). In this case it was assigned the row number 3 and the column number 0.

So for the moment nothing will be executed unless the “Tk()” function had been called, in this example it had been declared with a variable called “root”, like it’s shown in the line 19.

In the line 20 is declared the title of the window with the function title. Then comes the variables velo, diameter and nu, which are going to be of the float point nature and be assigned the DoubleVar() function. There is the IntVar() function for integers but that’s another story.

Page 6: Tkinter: example of use

And now lets talk about how the window is being build. In the next image we have pointed the labels, those are made it with the function Label(), then the Entry Box with the function Entry(), and also the Button is created with a function called Button().

So, for example we have the corresponding code for the velocity “structure” like this:

And let’s point out that we are always indicating where to put every piece with the grid() function, or else the code will get random results to show in the window.

Page 7: Tkinter: example of use

Also there is the command that will be assigned to the button, in this case this will be to calculate the Reynolds number with the defined function in the first lines of the code, like it’s shown in the next image:

There is something that was changed in the whole code, and it’s about the height of every label, button, or entry box; well, this height is assigned automatically to a default value, but in this case it wasn’t necessary to mess with that feature.And finally it’s the line 73, in which it’s indicated that the window would be shown in the screen, the mainloop() function is supposed to update the screen at a certain speed, called “frames per second”.

So there is just left the matter of testing the code, run the code and it should be something like this:

Page 8: Tkinter: example of use

Once you click immediately the “Re” button you should get the next message in the lower left corner of the window:

But if you put some values you will get the value of the Reynolds Number:

Page 9: Tkinter: example of use

###--- Final Code-----####### Calculating Reynolds Number in a circular pipefrom Tkinter import *

def calc_Re(): """ Re : Reynolds Number Re = velocity * diameter / kinematic_viscosity """ if nu.get() ==0: print "Kinematic viscosity must be greater than zero" label_result = Label(root, text = 'Nu = 0 ') label_result.grid(row = 3, column = 0)

else: print velo.get() * diameter.get() / nu.get() label_result = Label(root, text = 'Re = ' + str(velo.get() * diameter.get() / nu.get() )) label_result.grid(row = 3, column = 0)

root = Tk()root.title('Calculating Reynolds Number in a circular pipe')velo = DoubleVar()diameter = DoubleVar()nu = DoubleVar() # this addedentry_width = 25

## ---- velocity boxes --------------##################cell_velo = Label(root, text = "VELOCITY :", width = 22)cell_velo.grid(row = 0, column = 0)

cell_velo_symbol = Label(root, text = "U", width = 7)cell_velo_symbol.grid(row = 0, column = 1)

velo_txt = Entry(root, textvariable = velo, width = entry_width)velo_txt.grid(row = 0, column = 2)

cell_velo_unities = Label(root, text = "m/s", width = 7)cell_velo_unities.grid(row = 0, column = 3)

## ---- diameter boxes --------------##################cell_diameter = Label(root, text = "DIAMETER :", width = 22)cell_diameter.grid(row = 1, column = 0)

cell_diameter_symbol = Label(root, text = "D", width = 7)cell_diameter_symbol.grid(row = 1, column = 1)

diamter_txt = Entry(root, textvariable = diameter, width = entry_width)diamter_txt.grid(row = 1, column = 2)

cell_diameter_unities = Label(root, text = "m", width = 7)cell_diameter_unities.grid(row = 1, column = 3)

## ---- kinematic viscosity boxes --------------##################cell_nu = Label(root, text = "KINEMATIC VISCOSITY :", width = 22)cell_nu.grid(row = 2, column = 0)

cell_nu_symbol = Label(root, text = "Nu", width = 7)cell_nu_symbol.grid(row = 2, column = 1)

nu_txt = Entry(root, textvariable = nu, width = entry_width) # this addednu_txt.grid(row = 2, column = 2) # this added

cell_nu_unities = Label(root, text = '(m^2)/s', width = 7)cell_nu_unities.grid(row = 2, column = 3)

## ---- Re button box -------------##############################cell_re_calc = Button(root, text = "Re", command = calc_Re, width = 7)cell_re_calc.grid(row = 3, column = 1)

cell_re_tip = Label(root, text = 'Click on Re to calculate')cell_re_tip.grid(row = 3, column = 2) # this added

##---- main loop ----##############root.mainloop()