Sie sind auf Seite 1von 9

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. .! "#$ The code was make it in %indows &P Service Pack ' so if there is some (u) please contact with the author at : rider*+! ,yahoo.com' - would appreciate some politeness always. This document was done .ust with the solely purpose of showin) some of the ways in which can (e used Tkinter' the standard Python interface to the /Tk/ )raphical user interface 012-3 toolkit. -t4s e"pected that the reader most (e familiar with the (asis of Python.
05ots of information a(out it in the internet and in its official pa)e www.python.org/3

###---

The code -----#####

06ou can copy and paste the code that it7s in final pa)e of this document :P3

###--- Brief explanation of the code -----##### The purpose is to make a calculator of the 8eynolds 9um(er in a circular pipe with S- units.

The code will start callin) all from Tkinter

:nd also there must (e the declaration of the function Tk03 which )oes like :

This could have (een declared in the line (ut - considered (etter to put it after the definition of the function calc+8e03 Then it4s defined the function that will calculate the 8eynolds num(er with the form

8e ; velocity * diameter < kinematic+viscosity

%ell' in this case we use .ust one style of the ways in which is calculated the 8eynolds num(er. -n the line num(er = we start declarin) the function without any ar)ument at all.

>ut then we have a division' in this case is the value of nu' the kinematic viscosity' so we must make some e"ception in order to not )et the *ero division error' so - decided to .ust show a messa)e that says ?9u ; !@' so that could (e chan)ed to other stuff' like a pop up window that shows the alert or somethin) like that' so that is up to you. :nyway' all comes to a simple conditional' and the ?else@ part will (e the part in which the operations are made it. The line A5 is what will (e show in the python shell or command window or whateverBthatBtheB(lackBwindowBisBnamed. The line A$ will show the result in the window that will (e createdC the 5a(el function is in char)e of that. -n the ar)uments are reDuired ?root@' that is declared in the line AE' and the ?te"t@ that will (e shown in that area of the window.

%hen it comes to the 5a(el function' the first ar)ument would (e for the main window in which is shown the )raphical interface' and then comes the second ar)ument which in this case we named te"t and will (e a strin)' so the numeric values must (e chan)ed to strin) with the function str03. The other thin) is re)ardin) to the )rid issue' which must (e take it as a ta(le of e"cel 0or somethin) akin to that3. -n this case it was assi)ned the row num(er and the column num(er !.

So for the moment nothin) will (e e"ecuted unless the ?Tk03@ function had (een called' in this e"ample it had (een declared with a varia(le called ?root@' like it7s shown in the line AE.

-n the line 2! is declared the title of the window with the function title. Then comes the varia(les velo' diameter and nu' which are )oin) to (e of the float point nature and (e assi)ned the Fou(leGar03 function. There is the -ntGar03 function for inte)ers (ut that7s another story.

:nd now lets talk a(out how the window is (ein) (uild. -n the ne"t ima)e we have pointed the la(els' those are made it with the function 5a(el03' then the Hntry >o" with the function Hntry03' and also the >utton is created with a function called >utton03.

So' for e"ample we have the correspondin) code for the velocity ?structure@ like this:

:nd let7s point out that we are always indicatin) where to put every piece with the )rid03 function' or else the code will )et random results to show in the window.

:lso there is the command that will (e assi)ned to the (utton' in this case this will (e to calculate the 8eynolds num(er with the defined function in the first lines of the code' like it7s shown in the ne"t ima)e:

There is somethin) that was chan)ed in the whole code' and it7s a(out the hei)ht of every la(el' (utton' or entry (o"C well' this hei)ht is assi)ned automatically to a default value' (ut in this case it wasn7t necessary to mess with that feature. :nd finally it7s the line 7 ' in which it7s indicated that the window would (e shown in the screen' the mainloop03 function is supposed to update the screen at a certain speed' called ?frames per second@.

So there is .ust left the matter of testin) the code' run the code and it should (e somethin) like this:

Ince you click immediately the ?8e@ (utton you should )et the ne"t messa)e in the lower left corner of the window:

>ut if you put some values you will )et the value of the 8eynolds 9um(er:

###---

Final Code-----#####

JJ Kalculatin) 8eynolds 9um(er in a circular pipe from Tkinter import * def calc+8e03: /// 8e : 8eynolds 9um(er 8e ; velocity * diameter < kinematic+viscosity /// if nu.)et03 ;;!: print /Linematic viscosity must (e )reater than *ero/ la(el+result ; 5a(el0root' te"t ; 49u ; ! 43 la(el+result.)rid0row ; ' column ; !3 else: print velo.)et03 * diameter.)et03 < nu.)et03 la(el+result ; 5a(el0root' te"t ; 48e ; 4 M str0velo.)et03 * diameter.)et03 < nu.)et03 33 la(el+result.)rid0row ; ' column ; !3 root ; Tk03 root.title04Kalculatin) 8eynolds 9um(er in a circular pipe43 velo ; Fou(leGar03 diameter ; Fou(leGar03 nu ; Fou(leGar03 J this added entry+width ; 25 JJ BBBB velocity (o"es BBBBBBBBBBBBBBJJJJJJJJJJJJJJJJJJ cell+velo ; 5a(el0root' te"t ; /GH5IK-T6 :/' width ; 223 cell+velo.)rid0row ; !' column ; !3 cell+velo+sym(ol ; 5a(el0root' te"t ; /2/' width ; 73 cell+velo+sym(ol.)rid0row ; !' column ; A3 velo+t"t ; Hntry0root' te"tvaria(le ; velo' width ; entry+width3 velo+t"t.)rid0row ; !' column ; 23 cell+velo+unities ; 5a(el0root' te"t ; /m<s/' width ; 73 cell+velo+unities.)rid0row ; !' column ; 3 JJ BBBB diameter (o"es BBBBBBBBBBBBBBJJJJJJJJJJJJJJJJJJ cell+diameter ; 5a(el0root' te"t ; /F-:NHTH8 :/' width ; 223 cell+diameter.)rid0row ; A' column ; !3 cell+diameter+sym(ol ; 5a(el0root' te"t ; /F/' width ; 73 cell+diameter+sym(ol.)rid0row ; A' column ; A3 diamter+t"t ; Hntry0root' te"tvaria(le ; diameter' width ; entry+width3 diamter+t"t.)rid0row ; A' column ; 23 cell+diameter+unities ; 5a(el0root' te"t ; /m/' width ; 73 cell+diameter+unities.)rid0row ; A' column ; 3 JJ BBBB kinematic viscosity (o"es BBBBBBBBBBBBBBJJJJJJJJJJJJJJJJJJ cell+nu ; 5a(el0root' te"t ; /L-9HN:T-K G-SKIS-T6 :/' width ; 223 cell+nu.)rid0row ; 2' column ; !3 cell+nu+sym(ol ; 5a(el0root' te"t ; /9u/' width ; 73 cell+nu+sym(ol.)rid0row ; 2' column ; A3 nu+t"t ; Hntry0root' te"tvaria(le ; nu' width ; entry+width3 J this added nu+t"t.)rid0row ; 2' column ; 23 J this added cell+nu+unities ; 5a(el0root' te"t ; 40mO23<s4' width ; 73 cell+nu+unities.)rid0row ; 2' column ; 3 JJ BBBB 8e (utton (o" BBBBBBBBBBBBBJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ cell+re+calc ; >utton0root' te"t ; /8e/' command ; calc+8e' width ; 73 cell+re+calc.)rid0row ; ' column ; A3 cell+re+tip ; 5a(el0root' te"t ; 4Klick on 8e to calculate43 cell+re+tip.)rid0row ; ' column ; 23 J this added JJBBBB main loop BBBBJJJJJJJJJJJJJJ root.mainloop03

Das könnte Ihnen auch gefallen