Thursday 7 May 2015

Using tkMessageBox

Now I'm going to use the module "tkMessageBox", which is used to display message boxes, first in a simple code in order to show how it goes, then using it with a previous posted example :P.

The simple code has been a used with one of the functions that "tkMessageBox" provides (for a little bit more of information you can go to the next link : Python Tkinter tkMessageBox;  or this other one for the code of the message box of the first image in this post)

Anyway, this is the simple code (a warning will be shown when the user clicks the button that says "Warning"):

import Tkinter as tk
import tkMessageBox

root = tk.Tk()

def info():
   tkMessageBox.showwarning("Warning", "Resources changed")

tk.Button(root, text = "Pop up", command = info).pack()

root.mainloop()

This is how it looks:


The second code is a modification of a previous example (_!!!_), in this case the display box is for showing information.

Of course, this is the image to be used:

And this is the code:

from Tkinter import *
import tkMessageBox

def calc_Re():
    """
    Re : Reynolds Number
         (Adimensional number used to determine
          if a fluid flow is laminar or turbulent type)
    Re = velocity * diameter / kinematic_viscosity
    Re =  v__val  *  d__val  /      nu_val
    (row = 3, column = 1)
    """

    if nu_val.get() == 0:
        print "Kinematic viscosity must be greater than zero"
        lab4.config(text="Nu = 0")
        lab4.grid(row = 3, column =1)

    else:
        v_ = v__val.get()
        d_ = d__val.get()
        nu = nu_val.get()
        re_= v_ * d_ / nu
        result = "Re = " + str(re_)
        print "Re = {: f};(U = {: f}, D = {: f}, v = {: f})".format(re_, v_, d_, nu)
        lab4.config(text=result)
        lab4.grid(row = 3, column = 1)

def info():
   tkMessageBox.showinfo("Information", "Re: Reynolds number in a pipeline")

root = Tk()
root.title("Reynolds (Re)")

img_01 = PhotoImage(file = "_Re_01_54x25.gif")
v__val = DoubleVar()
d__val = DoubleVar()
nu_val = DoubleVar()

lab1 = Label(root, text = "velo: U =").grid(row = 0, column = 0)
lab2 = Label(root, text = "diam: D =").grid(row = 1, column = 0)
lab3 = Label(root, text = "nu__: v =").grid(row = 2, column = 0)
lab4 = Label(root, text  = " ")

ent1 = Entry(root, textvariable = v__val).grid(row = 0, column = 1)
ent2 = Entry(root, textvariable = d__val).grid(row = 1, column = 1)
ent3 = Entry(root, textvariable = nu_val).grid(row = 2, column = 1)

btn1 = Button(root, text = "Info" , command = info, bg = "black", fg = "yellow").grid(row = 2, column = 2)
btn2 = Button(root, image = img_01, command = calc_Re).grid(row = 3, column = 0)

root.mainloop()
The modifications were the implementation of the "info" function and the button with the same name, with changes in its colors.
This is the result if the button is clicked:

No comments:

Post a Comment