tag:blogger.com,1999:blog-32500119854833933062024-02-07T17:09:34.405-08:00Python & TKinter StuffTkinter with Python codes as guidelines/tutorialsJenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.comBlogger22125tag:blogger.com,1999:blog-3250011985483393306.post-77053705279963047562019-02-13T16:16:00.000-08:002019-02-13T16:24:27.706-08:00Changing Colours<head>
<style>
body {background-color:black}
h1 {color:blue}
p {color:green}
pre {color:white}
</style>
</head>
<body>
<img alt="img_a_01" src="https://2.bp.blogspot.com/-
lQhCMx1kTVg/XGSwKCXVoOI/AAAAAAAAAgc/m-
UL6mqRKIIkzuRyGXN9SLoxIykQo5xIgCLcBGAs/s160
0/22_a_01.png" style="height: 222px; width: 217px;" />
<br />
<p> Well, in this opportunity I got some simple animation that involves a straight line that change its colour between 4 options in a fixed order, it's a relatively simple task, although I've seen codes that involve the use of objects and classes, so I hope to get a better understanding on them before posting about it. </p>
<a name='more'></a>
<p> Anyway, the next are the code lines used for the changing colours animation: </p> <br />
<blockquote>
<pre>
import time
from Tkinter import *
root = Tk()
c1 = Canvas(root, height = 200, width = 200, bg =
"black")
colours = ["white", "red", "yellow", "blue"]
c1.pack()
for i in range(0, 1000):
for sprite in colours:
c1.delete(ALL)
c1.create_line(40, 50, 100, 110, fill = sprite ,
width = 3)
c1.update()
time.sleep(0.9)
root.title("Lines")
root.mainloop()
</pre>
</blockquote>
<img alt="img_01" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrmNIjzv04zBNaLtWtyocpPqMFCf0yn4wDLMXtcOXLGoInsdJIQ6fdRFdGEosT7PkdxFj-qBtJEV5lQLStEwndDpdMTd-5EGDGq7-FWe9-h546PxM1mlyLi3-XeJiIdgxRBy7jmtPEYnE/s1600/22th_gif_01_%25281FPS%2529.gif" style="height: 379px; width: 516px;" />
<br />
<p>:D</p>
<br />
</body>
Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com0tag:blogger.com,1999:blog-3250011985483393306.post-34158341645861153442017-08-08T14:53:00.001-07:002017-08-08T15:33:43.477-07:00Mach Number Calculator<head>
<style>
body {background-color:black}
h1 {color:blue}
p {color:green}
pre {color:white}
</style>
</head>
<body>
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjE6ZmCWjX11Fq1SK1dd2JUtx_dCWHZ7xgOKRDP_rU99FuxxqeZ8ZUaEbRERD5i_TguRMaYz0hnrlOMRdbKZRaxm-rIFU4DO7zK1qiDzxf1cqznLy4OQEyjQPupiRGaGZtHILbULRfuCHk/s1600/21_a_01.png" alt="img_a_01" style="width:237px;height:226px">
<p> Code for the simple Mach Number calculator:</p>
<blockquote>
<pre>
from Tkinter import *
def mc():
# 1st Function
try:
ns00, ns01 = a00.get(), a01.get()
ns02, ns03 = a02.get(), a03.get()
V = ns00
k = ns01
R = ns02
T = ns03 <a name='more'></a>
""" M : Mach Number """
M = V / (k * R * T) ** 0.5
a = "M = " + "{:10.10f}".format(M)
except ZeroDivisionError:
a = "Zero Div Error"
print "Zero Division Error"
except ValueError:
a = "Value Error"
print "Value Error"
ns1.config(text = a)
ns1.grid(row = 5, column = 1, sticky = W)
def ent(var, row, col, width = None):
# 2nd Function
w = Entry(root, textvariable = var, width = 19)
w.grid(row = row, column = col)
return w
def tx7(x, y, txt):
# 3rd Function
w = c1.create_text(x, y, anchor = W, font="Impact", text = txt)
return w
def txt(x, y, txt, size, colour):
c1.create_text( x, y,
anchor = W,
font = ("Verdana", size),
text = txt,
fill = colour)
def line(x0, y0, color, x1, y1):
# 4th Function
w = c1.create_line(x0, y0, x1, y1, fill = color)
return w
def btn(txt, row, col, cmd = None):
# 5th Function
w = Button(root, text = txt, command = cmd)
w.grid(row = row, column = col)
return w
def lab(txt, row, col):
# 6th Function
w = Label(root, text = txt)
w.grid(row = row, column = col)
return w
def frame_c(c1, c2):
# 7th Function
line(x0, y0, c1, x3, y3), line(x0, y0, c2, x1, y1)
line(x2, y2, c1, x1, y1), line(x2, y2, c2, x3, y3)
root = Tk()
ch, cw = 70, 200
x0, y0 = 02 , 02
x1, y1 = 02 , ch - 2
x2, y2 = cw - 2, ch - 2
x3, y3 = cw - 2, 02
c1 = Canvas(root, height = ch, width = cw)
frame_c("blue", "green")
a0, b0 = 5, 12
line(112 + a0, 32 + b0, "red", 116 + a0, 45 + b0)
line(116 + a0, 24 + b0, "red", 116 + a0, 45 + b0)
line(116 + a0, 24 + b0, "red", 165 + a0, 24 + b0)
tx7(20, 30, "M"), tx7(40, 30, "=")
tx7(59, 22, "_"), tx7(80, 30, "=")
tx7(60, 20, "V"), tx7(60, 40, "c")
tx7(144, 22, "_"), tx7(125, 49, "k")
tx7(140, 20, "V"), tx7(140, 49, "R")
tx7(157, 49, "T"), tx7(137, 22, "_")
tx7(130, 22, "_"), tx7(121, 22, "_")
tx7(151, 22, "_"), tx7(158, 22, "_")
tx7(114, 22, "_"), tx7(165, 22, "_")
c1.grid(row = 0, column = 0, columnspan = 3)
''' 0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,2
3,0 3,1 3,2
4,0 4,1 4,2
'''
a00, a01 = DoubleVar(), DoubleVar()
a02, a03 = DoubleVar(), DoubleVar()
v1, k1, r1, t1, e1 = "V =", "k =", "R =", "T =", "Execute"
s0, s1, s2, s3, s4 = "m/s", " ", "J/(kg-K)", " K", "<>"
lab(v1, 1,0) ; ent(a00, 1,1) ; lab(s0, 1,2)
lab(k1, 2,0) ; ent(a01, 2,1) ; lab(s1, 2,2)
lab(r1, 3,0) ; ent(a02, 3,1) ; lab(s2, 3,2)
lab(t1, 4,0) ; ent(a03, 4,1) ; lab(s3, 4,2)
btn(e1, 5,0, mc); Label(root) ; lab(s4, 5,2)
ns1 = Label(root)
root.title("Mach Number")
root.mainloop()
</pre>
</blockquote>
<img src="https://1.bp.blogspot.com/-EQaFJ-
EX9l8/WYo5UqxBycI/AAAAAAAAAd0/n4wOUM8Yf5Iw2D4PQoawDA1JbTReM8
czACLcBGAs/s1600/21th_gif_01_%25281FPS%2529.gif"
alt="img_01" style="width:233px;height:225px">
<p>(Further reading of the Mach number is kind of easy to be found :D).
<br>
</body>Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com4tag:blogger.com,1999:blog-3250011985483393306.post-49986540346313323522016-12-01T19:03:00.000-08:002017-03-04T16:09:12.583-08:00Canvas - Text<head>
<style>
body {background-color:black}
h1 {color:blue}
p {color:green}
pre {color:white}
</style>
</head>
<body>
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxjiL8oCPeotU6zmv-IY4EY1FiiSADZRqbAvpJrRM-K8Vpc_eB3oQc8dN_ZZnoGjMJj6H0OcBVvaahhgPZOIPZenpQGF8BKy1YtEfaJJDozie1a47oh2NPZ-bhzqPFcGwg-LYoWrfnbX4/s1600/20_a_01.png" alt="img_a_01" style="width:225px;height:225px">
<p>The first code shows how the text is located in the canvas:</p>
<blockquote>
<pre>
from Tkinter import *
root = Tk()
ch, cw = 200, 200
x0, y0 = 0, 0
x1, y1 = 20, 40 <a name='more'></a>
bgc = "black" # bgc : background colour
c1 = Canvas(root, height = ch, width = cw, bg = bgc)
c1.create_text( x1, y1, anchor = W, font= "Verdana" ,
text = "Hey there!", fill="white" )
c1.create_rectangle( x0, y0, x1, y1,
fill = "blue",
outline = "cyan")
c1.pack()
root.title("Draft")
root.mainloop()
</pre>
</blockquote>
<p><b>We must create a canvas using the "Canvas" method, also its dimensions are declared here.</b>
<br>
<br>c1 = Tkinter.Canvas(root, height = ch, width = cw, bg = "black")
<br>
<br><b>Then we create text by using:</b>
<br>
<br>c1.create_text( x1, y1, anchor = W, font = "Verdana" , text = "Hey there!", fill="white")
<br>
<br><b>The text will be fitted inside an invisible rectangle, but the position will refered with respect
to the intersection point between the left side line of the rectangle and the line that divides the
rectangle in half, and then the location of this intersection point will be fixed by (x1, y1). In this
case, it's been used (x1,y1) = (20; 40) as the position of such intersection point.</b>
<br>
<br>(x1,y1) = (20, 40)
<br>
<br><b>To show what has been explained of the intersection point, there's a graphical example that
uses the drawing of a rectangle that uses the origin point (0; 0) and the point (x1, y1) as its
corners.Then we could show the results in the screen by using the "pack" or "grid" methods.</b>
</p>
<img src="https://1.bp.blogspot.com/-kpCAVQ-
Vz_Q/WEDiSutzxzI/AAAAAAAAAbg/JYJqlc57RcgWtWMbO_flSgqvAEptc5NAACLcB/s1600/20th_g
if_01_%25281FPS%2529.gif" alt="img_01" style="width:473px;height:374px">
<p>The next examples have some variations.
<br>
<br> Second code sample is:
</p>
<blockquote>
<pre>
from Tkinter import *
root = Tk()
s = 20
ch, cw = 200, 200
x, y, n = 0, 90, 11
bgc = "black" # bgc : background colour
cr1 = "white"
cr2 = "red"
strin6 = '''Python 2.7.9
(
default\t\t,
Dec 10 2014\t,
12:24:55
)'''
c1 = Canvas(root, height = ch, width = cw, bg = bgc)
c1.create_text(20, 90, anchor = W, font=("Verdana", n), text = strin6, fill="white")
c1.pack()
root.title("Dr4f7")
root.mainloop()
</pre>
</blockquote>
<img src="https://1.bp.blogspot.com/-
or6KxNrsQOA/WEDiRI6EsxI/AAAAAAAAAbY/XChNHZDKFR4L9RK9T19NnfYVwiioriucgCLcB/s1600
/20_b_01.png" alt="img_01" style="width:228px;height:249px">
<p>Third code sample is:
</p>
<blockquote>
<pre>
from Tkinter import *
def line(a, b, color, w):
x0, y0, x1, y1 = a[0], a[1], b[0], b[1]
c1.create_line(x0, y0, x1, y1, fill = color, width = w)
def txt(x, y, txt, size, colour):
c1.create_text( x, y,
anchor = W,
font = ("Verdana", size),
text = txt,
fill = colour)
def n(a0, L, w):
x0, y0 = a0[0], a0[1]
x1, y1 = x0 + L, y0 + L
dx, dy = L / 5.0, L / 5.0
a1, b1 = [x0 - dx, y0 - dy], [x1 + dx, y1 + dy]
a2, b2 = [a1[0], b1[1]], [b1[0], a1[1]]
r1, r2 = "orange", "white"
c1.create_oval(x0, y0, x1, y1, outline = r1, fill = r2, width = w)
line(a2, b2, r1, w)
print a2, "\n", b2
root = Tk()
n_t1 = 14
n_t2 = 9
ch, cw = 600, 350
x0, y0 = 20, ch*0.5
bgc = "black" # bgc : background colour
cr1 = "white"
cr2 = "red"
a, b = [2 * x0, ch - 20], [cw - 2 * x0, ch - 20]
c, d = [2 * x0, ch - 30], [cw - 2 * x0, ch - 30]
e1 = [c[0] - 28, c[1] - 3]
e2 = [d[0] + 10, d[1] - 3]
title = '''
Joydrop - Breakdown
(Lyrics)
'''
lyrics = '''
And I don't hear you anymore
And I can't see you on the floor
It's like a nightmare come to life
But don't be frightened of the light
And all the days you pushed around
And all the times you lied to yourself
And all the dreams we had just might breakdown
woooo--ooo
Breakdown
woooo--ooo
And I wonder who you love, really love
You know I want you too much
You know it kills me the thought
I want to scream, I want to cry
Just to save myself just one last time
And all the days you pushed around
And all the times you lied to yourself
And all the dreams we had just might breakdown
woooo--ooo
Breakdown
woooo--ooo
And I wonder who you love, really love
And all the days you pushed around
And all the times you lied to yourself
And all the dreams we had just might breakdown
woooo--ooo
Breakdown
woooo--ooo
Breakdown
woooo--ooo
And I wonder who you love, really love
'''
c1 = Canvas(root, height = ch, width = cw, bg = bgc)
txt(x0, 1.35*x0, title, n_t1, "red")
txt(x0, y0, lyrics, n_t2, "white")
line(a, b, "orange", 1)
line(c, d, "orange", 1)
#--- circle symbols ---#
n(e1, 18, 2)
n(e2, 18, 2)
c1.pack()
root.title("Joydrop - Breakdown (Lyrics)")
print a, b
print c, d
root.mainloop()
</pre>
</blockquote>
<img src="https://3.bp.blogspot.com/-
aLSZo1RGgd4/WEDiRpV6eeI/AAAAAAAAAbc/jEHCYHpaUh0d-
QeoC5Iqjp8Hrgx6DACswCLcB/s1600/20_c_01.png" alt="img_01" style="width:368px;height:641px">
</body>Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com2tag:blogger.com,1999:blog-3250011985483393306.post-40337796420456054292016-07-26T17:45:00.001-07:002017-06-04T20:22:47.039-07:00Canvas - Drawing Polygons<head>
<style>
body {background-color:black}
h1 {color:blue}
p {color:green}
pre {color:white}
</style>
</head>
<body>
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGYpSwWAnIcy_sBzjrD_a7pRfaKRLkwpH70iC69hKr6L0OMyFCs6F2dGaXTE2cpVVUmz2UESrzhhwru2UN_7SIiMQ8itoNekWIDJmumQBqo7QQFg_kJK-zZ1rjsu14lFStz31dqaoUKM4/s1600/19_a_01.png" alt="img_a_01" style="width:197px;height:197px">
<p>The first code is a simple example:</p>
<blockquote>
<pre>
import Tkinter
root = Tkinter.Tk()
ch, cw = 170, 170
c1 = Tkinter.Canvas(root, height = ch, width = cw, bg = "black")
ps = [ 60, 130, 40, 120, 40, 80, 70, 40,
100, 40, 130, 80, 130, 120, 110, 130] <a name='more'></a>
c1.create_polygon(ps, outline = 'yellow', fill = 'teal', width = 2)
c1.pack()
root.title("BckP")
root.mainloop()
</pre>
</blockquote>
<p><b>We must create a canvas using the "Canvas" method, also its dimensions are declared here.</b>
<br>c1 = Tkinter.Canvas(root, height = ch, width = cw, bg = "black")
<br>
<br><b>Then we create a polygon by using:</b>
<br>c1.create_polygon(points, outline='yellow', fill='teal', width=2)
<br>
<br><b>The coordinates of the points that define the polygon are in the "ps" list:</b>
<br>
<br>ps = [ 60, 130, 40, 120, 40, 80, 70, 40,
100, 40, 130, 80, 130, 120, 110, 130]
<br>
<br><b>Then we could show the results in the screen by using the "pack" or "grid" methods.</b>
</p>
<img src="https://3.bp.blogspot.com/-nahQ61_oGjQ/V5gCRpjys-
I/AAAAAAAAAUc/8IYgA6_M0JIsW4jWWCJ8zHcijgyREhV7gCLcB/s1600/19th_gif_01_
%25281FPS%2529.gif" alt="img_01" style="width:574px;height:278px">
<p>The next examples have some variations.
<br> Second code sample is:
</p>
<blockquote>
<pre>
import Tkinter
root = Tkinter.Tk()
ch, cw = 200, 200
a, b = 40, 40
bgc = "blue" # bgc : background colour
cr1 = "brown"
cr2 = "yellow"
c1 = Tkinter.Canvas(root, height = ch, width = cw, bg = bgc)
points = [ 60 + a, 130 + b, 40 + a, 120 + b,
40 + a, 80 + b, 70 + a, 40 + b,
100 + a, 40 + b, 130 + a, 70 + b,
2 + a, 0 + b, 0 + a, 0 + b]
c1.create_polygon(points, outline = cr1, fill = cr2, width = 2)
c1.pack()
root.title("Dmd01")
root.mainloop()
</pre>
</blockquote>
<img src="https://2.bp.blogspot.com/-TAac7FzM98U/V5gCP-
kRsdI/AAAAAAAAAUU/I6JwyJHrocUiiQNnJ-wloD_zzEyF7A_dQCLcB/s1600/19_b_01.png"
alt="img_01" style="width:232px;height:252px">
<p>Third code sample is:
</p>
<blockquote>
<pre>
import Tkinter
def tr(a, b, m, n):
ax, ay = [ 0 * m + a, 0 * n + b]
bx, by = [ 0 * m + a, 20 * n + b]
cx, cy = [17 * m + a, 10 * n + b]
ps = [ax, ay, bx, by, cx, cy]
c1.create_polygon(ps, outline = cr1, fill = cr2, width = 2)
root = Tkinter.Tk()
s = 20
ch, cw = 200, 200
a, b, n = 40, 30, 14
bgc = "black" # bgc : background colour
cr1 = "white"
cr2 = "red"
c1 = Tkinter.Canvas(root, height = ch, width = cw, bg = bgc)
# Lil' triangles array
for i in range(17):
tr(20 + 10 * i, 20, 0.5, 0.5)
tr(20 + 10 * i, 180, 0.5, 0.5)
# Parts of a big triangle
tr(45 + s, 45, 2.5, 2.5)
tr(95 + s, 75, 2.5, 2.5)
tr(45 + s, 105, 2.5, 2.5)
c1.pack()
root.title("TrG1")
root.mainloop()
</pre>
</blockquote>
<img src="https://3.bp.blogspot.com/-wC3Zhfo1mvM/V5gCQEH5_2I/AAAAAAAAAUY/oraL-
w2HtMo9oaBYdyF5Bs2izA1G9VnOgCLcB/s1600/19_c_01.png" alt="img_01"
style="width:234px;height:258px">
</body>Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com0tag:blogger.com,1999:blog-3250011985483393306.post-69306951901531377942016-07-21T11:26:00.000-07:002017-06-04T20:11:00.790-07:00Canvas - Drawing Arcs<head>
<style>
body {background-color:black}
h1 {color:blue}
p {color:green}
pre {color:white}
</style>
</head>
<body>
<img alt="img_01v2" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6uDyuejHUv3FuH8XKQeBpWNxIH2jdlQ2igHzrZfC_jyiUkMNYzES7uD3AUHB-hNZ5F1PpKdlz4SDulG3HreJ9HRV7exitJ9PtlQs2nEdWO2q4NXGeMlqVz7MX01LiHPdPVkNOxVHC76I/s1600/18_01_v2.png" style="height: 227px; width: 227px;" />
<br />
The first code is a simple example:<br />
<blockquote>
<pre>
import Tkinter
root = Tkinter.Tk()
ch, cw = 200, 200
c1 = Tkinter.Canvas(root, height = ch, width = cw, bg = "black")
c1.create_arc(30, 30, 200, 200, start = 30, extent = 45, outline = "brown",
fill = "yellow", width = 4)<a name='more'></a>
c1.create_arc(10, 130, 200, 230, start = 30, extent = 145, outline = "brown",
fill = "yellow", width = 4)
c1.create_arc(30, 30, 100, 100, start = 30, extent = 295, outline = "green",
width = 2)
c1.create_arc(50, 50, 65, 60, start = 30, extent = 310, outline = "green",
width = 2)
c1.pack()
root.title("Arcs")
root.mainloop()
</pre>
</blockquote>
We must create a canvas using the "Canvas" method, also its dimensions are
declared here.<br />
<!-- -->
<!-- -->c1 = Tkinter.Canvas(root, height = ch, width = cw, bg = "black")
<br />
Then we create an arc by using:<br />
<!-- -->
<!-- -->c1.create_arc(30, 30, 200, 200, start = 30, extent = 45, outline = "brown", fill =
"yellow", width = 4)
<br />
The coordinates of the first point are (30, 30), and of the last point are (200,
200). The attributes of outline_color and width_size go with "outline" and "width",
respectively.<br />
Then we could show the results in the screen by using the "pack" or "grid"
methods.
<br />
<img alt="img_01" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFywZCci0zoBav6drX1cHeJCO59V97-TcsJVg7NjfZt5OK8TyBsnK1A8lHo9s-YDj6iXRo5C1pjXXvrU98UJ2GmmpcDZc6_rH2ODQ2Jhe7biT9nRp-BCuCu_d8gcWbIDgXO-03TJXUpIY/s1600/18th_gif_01_%25281FPS%2529.gif" style="height: 258px; width: 670px;" />
<!-- -->
The next code has some variations.<br />
<!-- -->
<blockquote>
<pre>
import Tkinter
def mz(x, y):
# Oval within a fixed space
c1.create_oval(x, y, x + 20, y + 10, fill = "red")
def arc(ax, ay, bx, by, s, e, w, o, f = None):
c1.create_arc( ax, ay, bx, by ,
start = s ,
extent = e ,
outline = o ,
fill = f ,
width = w )
root = Tkinter.Tk()
ch, cw = 200, 200
c1 = Tkinter.Canvas(root, height = ch, width = cw, bg = "black")
arc(30, 30, 200, 200, 30, 45, 4, "brown", "yellow")
arc(10, 130, 200, 230, 30, 145, 4, "brown", "yellow")
arc(50, 50, 65, 60, 30, 310, 2, "white", "green" )
arc(30, 30, 100, 100, 30, 295, 2, "green")
pts = [ [ 35, 150], [55, 160], [ 60, 140], [ 85, 160],
[ 90, 142], [115, 155], [120, 140], [125, 85],
[133, 65], [138, 45], [143, 145], [160, 65]
]
for n in range(len(pts)):
mz(pts[n][0], pts[n][1])
c1.pack()
root.title("Arcs")
root.mainloop()
</pre>
</blockquote>
<img alt="img_01v2" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6uDyuejHUv3FuH8XKQeBpWNxIH2jdlQ2igHzrZfC_jyiUkMNYzES7uD3AUHB-hNZ5F1PpKdlz4SDulG3HreJ9HRV7exitJ9PtlQs2nEdWO2q4NXGeMlqVz7MX01LiHPdPVkNOxVHC76I/s1600/18_01_v2.png" style="height: 227px; width: 227px;" />
<!-- -->
Second code sample is: <br />
<!-- -->
<blockquote>
<pre>
import Tkinter
def arc_fi(a, b, s, e, w):
ax, ay = a[0], a[1]
bx, by = b[0], b[1]
fc = "yellow" # fill color
c1.create_arc(ax, ay, bx, by, start = s, extent = e, fill = fc, width = w)
root = Tkinter.Tk()
ch, cw = 200, 200
c1 = Tkinter.Canvas(root, height = ch, width = cw, bg = "black")
for i in range(4, 7):
a = [10 * i, 10 * i]
b = [40 * i, 40 * i]
s = 15 * i
e = s + 15
arc_fi(a, b, s, e, 4)
c1.pack()
root.title("Rnd01")
root.mainloop()
</pre>
</blockquote>
<img alt="img_02" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjL8OFimurL2DC8ijCmG5x0s87iZ_pAjoSDYZyU1SZN4Ei0Jqn9iBV1fCoHnftsLtEHlQmEOgceQXJDuPhfsj6qoNT5EtJjbkl-vVKM9M46B7Dt4ihXwRgXjSYUNfnRvZr7iGzJaCZEdZQ/s1600/18_b_01.png" style="height: 252px; width: 230px;" />
Third code sample is: <br />
<!-- -->
<blockquote>
<pre>
import Tkinter
def arc_of(a, b, s, e, w):
ax, ay = a[0], a[1]
bx, by = b[0], b[1]
pts = [ax, ay, bx, by]
oc = "brown" # outline color
fc = "yellow" # fill color
c1.create_arc(pts, start = s, extent = e, outline = oc, fill = fc, width = w)
root = Tkinter.Tk()
ch, cw = 200, 200
c1 = Tkinter.Canvas(root, height = ch, width = cw, bg = "black")
for i in range(0, 4):
a = [30, 30]
b = [170, 170]
s = 30 * i
e = s + 30
arc_of(a, b, s, e, 2)
c1.pack()
root.title("Sections")
root.mainloop()
</pre>
</blockquote>
<img alt="img_c_01" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVTrXxKd1aVe4ucHHJTTjYRnjks72sHwYNO7vEwZzvfmDdml15pi99L_loXyzi3YBJ-QkxKsZ6895um8oKKeTZbVXXso5pbeK_MeMa2iidPrD2nyLgCWINMw_k_eCDETbv8x35kRT4tpY/s1600/18_c_01.png" style="height: 252px; width: 230px;" />
</body>Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com0tag:blogger.com,1999:blog-3250011985483393306.post-3149858138346662712016-07-02T20:52:00.000-07:002016-07-26T19:09:38.140-07:00Canvas - Drawing Ovals<head>
<style>
body {background-color:black}
h1 {color:blue}
p {color:green}
pre {color:white}
</style>
</head>
<body>
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2EozGOujHx6qIqoD0J85iY0F7J2papDUM1xH0O3gpNH1iCXGZT2Ys-QX-i5Ec_n6qXnQL4ue0aIGz8gB1u3RaoqAf8xkY_Gs9vEQmCUGiYqzmlYoxhxynis3WadmKQx8HDL2qRBJkZFY/s1600/17_a_01.png" alt="img_a_01" style="width:236px;height:260px">
<p>The first code is a simple example:</p>
<blockquote>
<pre>
import Tkinter
root = Tkinter.Tk()
ch, cw = 200, 200
c1 = Tkinter.Canvas(root, height = ch, width = cw, bg = "black")
c1.create_oval(30, 30, 175, 175, outline = "yellow", width = 2) <a name='more'></a>
c1.create_oval(30, 30, 90, 90, outline = "red", fill = "blue", width = 2)
c1.create_oval(130, 130, 170, 170, outline = "blue", fill = "red", width = 2)
c1.pack()
root.title("Ovals")
root.mainloop()
</pre>
</blockquote>
<p>We must create a canvas using the "Canvas" method, also its dimensions are declared here.
<br>
<br>c1 = Tkinter.Canvas(root, height = ch, width = cw, bg = "black")
<br>
<br>Then we create an oval by using:
<br>
<br>c1.create_oval(30, 30, 175, 175, outline = "yellow", width = 2)
<br>
<br>The coordinates of the first point are (30, 30), and of the last point are (175, 175). The attributes of outline_color and width_size go with "outline" and "width", respectively, and if there is no fill color declared, the oval will be just a colored ring.
<br>
<br>Then we could show the results in the screen by using the "pack" or "grid" methods.
</p>
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCJ3IBKZaTKV9wyoZAST4ck09VYuVhdMtJc_IqqETuzZUSzXBfd5j9aVTlXmz_ClExiBbwpO6_0W6H3dXd2tJnhJTYwmIhyphenhyphenI5DQM8Hpmz8O-gufD_aR_vNcta1FxbX-rYQZw4CCGtyVWI/s1600/17th_gif_01_%25281FPS%2529.gif" alt="img_01" style="width:584px;height:300px">
<p>The next examples are a little more complex but show something else.
<br> Second code sample is:
</p>
<blockquote>
<pre>
import Tkinter
def f(x1, y1, x2, y2):
r1, b1 = "red", "blue"
c1.create_oval(x1, y1, x2, y2, outline = r1, fill = b1, width = 2)
root = Tkinter.Tk()
ch, cw = 200, 200
c1 = Tkinter.Canvas(root, height = ch, width = cw, bg = "black")
for i in range(7):
a = 0.5
f(30 + 3*a*i, 30 + a*i*i*(-1**i), 90 + 3*a*i, 90 + a*i*i)
f(100 + 3*i, 100 + 3*i, 120 + 3*i, 120 + 3*i)
f(120 + 3*i, 90 + 3*i, 125 + 3*i, 140 + 3*i)
c1.pack()
root.title("Ovals_abstract")
root.mainloop()
</pre>
</blockquote>
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGhtMRZGqMzNKI6Es5GHQx2GVXOJBGaTn_KOsrJOLhyphenhyphenAjhnoIEygLUNYXb46T8pEGg3iFGzOW5xEAh41vboHWXz0z531qLe4td27H7pJrYqZIHc5QW5Ocjw95mnUfvSg7uHKt5N2QYruo/s1600/17_b_01.png" alt="img_01" style="width:228px;height:254px">
<p>Third code sample is:
</p>
<blockquote>
<pre>
import Tkinter
# line: function that draws a red line
def line(a, b):
x0, y0, x1, y1 = a[0], a[1], b[0], b[1]
c1.create_line(x0, y0, x1, y1, fill = "red", width = 1)
# n : function that draws a blue circle
def n(x1, y1, x2, y2, w):
r1 = "blue"
c1.create_oval(x1, y1, x2, y2, outline = r1, width = w)
root = Tkinter.Tk()
ch, cw = 300, 300
rm = 10
A = (0.5 * cw, rm )
B = (0.5 * cw, ch - rm )
C = (rm , 0.5 * ch)
D = (cw - rm , 0.5 * ch)
c1 = Tkinter.Canvas(root, height = ch, width = cw, bg = "black")
for i in range(1, 6):
if i < 4:
a, w = 7, 1
else:
a, w = 25, 6
n(ch * 0.5 - a*i, cw * 0.5 - a*i, ch * 0.5 + a*i, cw * 0.5 + a*i, w)
a, b = ch * 0.5 - a*i, cw * 0.5 - a*i
c, d = ch * 0.5 + a*i, cw * 0.5 + a*i
# print i, a, b
# print i, c, d
line(A, B)
line(C, D)
n(50, 50, 250, 250, 6)
c1.pack()
root.title("Ovals PTR")
root.mainloop()
</pre>
</blockquote>
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVXPCnen7CKwq-Dfvzjk-2kLjhRbYKylvPmesmKImc8s5_Hx22LuQwngunQ23AOQwU15jUu0m300ovnNLKFLu1oBlVT_ScV0hEGDUEW54XfX9Llueifa9Fzsse4Pu11Jeoz1z976blM_0/s320/17_c_01.png" alt="img_c_01" style="width:298px;height:320px">
</body>Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com0tag:blogger.com,1999:blog-3250011985483393306.post-37861898820362283262016-03-10T14:55:00.001-08:002016-04-22T14:57:46.640-07:00Canvas - Drawing Rectangles<head>
<style>
body {background-color:black}
h1 {color:blue}
p {color:green}
pre {color:white}
</style>
</head>
<body>
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgly7YH02C5L22gfMi6BZWaS8fdXcIUX44h5pfsOISa_BfEL4zn8BTNCKFNL0gxCo7tCbmV0q12yTVgdxdm-o3-SQTepGM5heucpLAIUvdC8K2EjxGvw_jeLxl6ZU_zJ6pGz0293MKLa8Q/s1600/16_10_001001001.png" alt="img_16_10_001001001" style="width:240px;height:240px">
<p>The first code is a simple example:</p>
<blockquote>
<pre>
from Tkinter import *
root = Tk()
c1 = Canvas(root, height = 180, width = 200, background = "black" )
c1.create_rectangle( 27, 17, 103, 93, fill = "blue", outline = "cyan")
c1.create_rectangle(103, 93, 179, 169, fill = "red", outline = "yellow")
c1.pack()
root.title("Rectangles")
root.mainloop()
</pre>
</blockquote>
<a name='more'></a><br />
<p>We must create a canvas using the "Canvas" method, also its dimensions are declared here.
<br>
<br>c1 = Canvas(root, height = 180, width = 200, background = "black" )
<br>Then we create a rectangle using:
<br>
<br>c1.create_rectangle( 27, 17, 103, 93, fill = "blue", outline = "cyan")
<br>
<br>The coordinates of the first point are (27, 17), and of the last point are (103, 93). The attributes of fill_color and outline_color go with "fill" and "outline", respectively.
<br>
<br>Then we could show the results in the screen by using the "pack" or "grid" methods.
</p>
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfGjn3kTaqyvt9HaYoiwyorxsyBvGSbrl8kZj2aSsNuV9MBy7vANSDs5FgGhOlIO74-jHetG7m6Hxr6Z4QeLmKVvRisHM4ioX5a3yV3xZ5CZv9A_i6vTiv-KhOi9kMGWlg_OnRyLZcFCs/s1600/16th_gif_01_%25281FPS%2529.gif" alt="img_01" style="width:554px;height:290px">
<p>The next example has many lines of code but show something else than just a couple of rectangles.
<br> Second code sample is:
</p>
<blockquote>
<pre>
import Tkinter
def line(a, b, color, w):
x0, y0, x1, y1 = a[0], a[1], b[0], b[1]
c1.create_line(x0, y0, x1, y1, fill = color, width = w)
def rect(a0, b0, outlin3, fill_, width_):
a, b, c, d = a0[0], a0[1], b0[0], b0[1]
o0, f0, w0 = outlin3, fill_, width_
# outline="#f11" fill="#1f1", width=2
c1.create_rectangle(a, b, c, d, outline = o0, fill = f0, width = w0)
def desktop_():
A, B = [16, 76], [179, 81]
rct_1 = rect(A, B, "black", "orange", 1)
def screen_():
A, B = [ 68, 20], [156, 64]
C, D = [ 70, 23], [153, 61]
E, F = [ 89, 73], [135, 76]
G, H = [110, 64], [114, 73]
rct_1 = rect(A, B, "black", "green", 1)
rct_1 = rect(C, D, "black", "black", 1)
rct_1 = rect(E, F, "black", "green", 1)
rct_1 = rect(G, H, "black", "green", 1)
def speakers_():
A, B = [ 52, 60], [ 64, 76]
C, D = [160, 60], [172, 76]
E, F = [ 54, 62], [ 62, 74]
G, H = [162, 62], [170, 74]
rct_1 = rect(A, B, "black", "green", 1)
rct_1 = rect(C, D, "black", "green", 1)
rct_1 = rect(E, F, "black", "gray", 1)
rct_1 = rect(G, H, "black", "gray", 1)
def cpu_():
A, B = [ 24, 28], [ 47, 76]
C, D = [ 33, 47], [ 37, 51]
E, F = [ 40, 48], [ 42, 50]
G, H = [ 28, 48], [ 30, 50]
I, J = [ 27, 39], [ 30, 42]
K, L = [ 34, 39], [ 37, 42]
M, N = [ 40, 39], [ 44, 42]
O, P = [ 27, 31], [ 44, 36]
Q, R = [ 27, 57], [ 44, 72]
rct_1 = rect(A, B, "black", "green", 1)
rct_1 = rect(C, D, "black", "black", 1)
rct_1 = rect(E, F, "blue", "blue", 1)
rct_1 = rect(G, H, "red", "red", 1)
rct_1 = rect(I, J, "black", "black", 1)
rct_1 = rect(K, L, "black", "black", 1)
rct_1 = rect(M, N, "black", "black", 1)
rct_1 = rect(O, P, "black", "blue", 1)
rct_1 = rect(Q, R, "black", "blue", 1)
root = Tkinter.Tk()
ch, cw = 100, 200
c1 = Tkinter.Canvas(root, height = ch, width = cw, bg = "white")
desktop_()
screen_()
speakers_()
cpu_()
c1.pack()
root.title("Good Old Desktop PC")
root.mainloop()
</pre>
</blockquote>
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEillvqR6PRQiXQ7k8HplLQfe_0D_EOIck27pHBNBFoGP5ZpJBruLI9WroQ5kuRhkS8epPUJ_EjiDHf4hu5ZvV6N4aqsM7a4vHsdVK1Ol-FO9Rkq0UW_X1fQzOvMfKddryAIDO3i2yjR2to/s1600/16_b_0101.png" alt="img_02" style="width:236px;height:154px">
<p>
<br> The third code sample is:
</p>
<blockquote>
<pre>
import Tkinter
def line(a, b, color, w):
x0, y0, x1, y1 = a[0], a[1], b[0], b[1]
c1.create_line(x0, y0, x1, y1, fill = color, width = w)
def pattern_1():
sx = sy = -20
dx = dy = 50
""" First Wave """
for j in range(3):
for i in range(3):
a = [sx + (1 + 2*i) * dx , sy + (1 + 2*j) * dx ]
b = [a[0] + dx, a[1] + dx]
rect(a, b, "white", "mediumseagreen", 3)
""" Second Wave """
a2 = [sx + 1.5 * dx , sy + 1.5 * dx]
b2 = [a2[0] + 4 * dx, a2[1] + 4 * dx]
rect(a2, b2, "white", "mediumseagreen", 3)
root = Tkinter.Tk()
ch, cw = 300, 300
c1 = Tkinter.Canvas(root, height = ch, width = cw, bg = "black")
pattern_1()
c1.pack()
root.title("SQ Pattern")
root.mainloop()
</pre>
</blockquote>
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhF4GLhiP-7Z39m-S6FLCFJCu2uSihSDgabQnqlcruJv0N7izaqqBCBm-yN-obg5ISQUl89rgDrBhQy-ZFCciN7TpJMWOyWnmmVLqC9uyiKK1KiZiZwz-TQpt5V3JjtlIyJClYIj3QQmkE/s1600/sqr_pattern.png" alt="img_03" style="width:338px;height:354px">
</body>
Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com0tag:blogger.com,1999:blog-3250011985483393306.post-50246460047530040352016-01-28T12:32:00.000-08:002016-07-02T08:09:55.212-07:00Canvas - Drawing Lines<head>
<style>
body {background-color:black}
h1 {color:blue}
p {color:green}
pre {color:white}
</style>
</head>
<body>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9ZDbCvMTaq9TX2iXil7fp_jpVxVmc1DzYzgoIDLi3tobpKn4_DPg_-HEzQWgCR45svyDmcafWCJiEk7c4naeP9plENryO7At4X29hXlRc4x2yHg7BIJHtTCtZI0Bamrd7qkvXZkUuxGk/s1600/15_c_01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9ZDbCvMTaq9TX2iXil7fp_jpVxVmc1DzYzgoIDLi3tobpKn4_DPg_-HEzQWgCR45svyDmcafWCJiEk7c4naeP9plENryO7At4X29hXlRc4x2yHg7BIJHtTCtZI0Bamrd7qkvXZkUuxGk/s1600/15_c_01.png" /></a><br />
The first code is a simple example:<br />
<blockquote>
<pre>
from Tkinter import *
root = Tk()
c1 = Canvas(root, height = 180, width = 200)
c1.create_line(20, 20, 160, 160, fill = "orange" , width = 3)
c1.create_line(40, 50, 100, 110, fill = "yellow" , width = 3)
c1.pack()
root.title("Lines")
root.mainloop()
</pre>
</blockquote>
We must create a canvas using the "Canvas" method, also its dimensions are declared here.
<a name='more'></a><br />
<br />c1 = Canvas(root, height = 180, width = 200)
<br />Then we create a line using:
<br />
<br />c1.create_line(20, 20, 160, 160, fill = "orange" , width = 3)
<br />
<br />The coordinates of the first point are (20, 20), and of the last point are (160, 160). The attributes of fill_color and line_width go with "fill" and "width", respectively.
<br />
<br />Then we could show the results in the screen by using the "pack" or "grid" methods.
<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvl4MPpDLB4k9rVGmvUdniGqbNn-BJUBrzxVayE5tKzUGZtEvHW3WU4z6tZcvYdodfIQIyfXZoIty83prtY29skfa1SePKJ0-c2OfLvvxWhY80Yts3NVSUkqpiqgIePHTM3d6ivxLB2Ek/s1600/15th_gif_01.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvl4MPpDLB4k9rVGmvUdniGqbNn-BJUBrzxVayE5tKzUGZtEvHW3WU4z6tZcvYdodfIQIyfXZoIty83prtY29skfa1SePKJ0-c2OfLvvxWhY80Yts3NVSUkqpiqgIePHTM3d6ivxLB2Ek/s1600/15th_gif_01.gif" /></a><br /> <br />
The next examples are a little more complex but show something else than just a couple of lines.
<br /> Second code sample is:
<br />
<blockquote>
<pre>
import Tkinter
def line(a, b, color, w):
x0, y0, x1, y1 = a[0], a[1], b[0], b[1]
c1.create_line(x0, y0, x1, y1, fill = color, width = w)
root = Tkinter.Tk()
ch, cw = 180, 200
A, B, C = [30, 16], [160, 16], [30, 165]
D, E, F = [160, 165], [ 20, 164], [90, 94]
G, H, I = [100, 85], [168, 16], [20, 16]
J = [168, 165]
P = [A, B, C, D, E, F, G, H, I, J]
c1 = Tkinter.Canvas(root, height = ch, width = cw)
for i in range(len(P) / 2):
if i < 2:
line(P[2 * i], P[2 * i + 1], "red" , 3)
else:
line(P[2 * i], P[2 * i + 1], "blue", 3)
c1.pack()
root.title("Lines")
root.mainloop()
</pre>
</blockquote>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0twKJcEytMyiGrU4r7zb1VMfkBbcw63BJ-eqqy2HehEA0tJwJOQHD9ADnWwx0gjTWhvaid5P5Z7uq2Hc_l1RTkznagTHfNDmq4TXxJKYFiBUwz31Zo8IRfihlzA26iFOceP6gV-_p7Ww/s1600/15_b_01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0twKJcEytMyiGrU4r7zb1VMfkBbcw63BJ-eqqy2HehEA0tJwJOQHD9ADnWwx0gjTWhvaid5P5Z7uq2Hc_l1RTkznagTHfNDmq4TXxJKYFiBUwz31Zo8IRfihlzA26iFOceP6gV-_p7Ww/s1600/15_b_01.png" /></a><br /> <br />
Third code sample is:
<br />
<blockquote>
<pre>
from Tkinter import *
root = Tk()
ch, cw = 180, 200
r , s = 5, 5
xs, xr = 5, 3
loop_b = 12
loop_c = 40
loop_d = 7
ox, oy = -20, -10
dx, dy = 5, 2.5
n = 1.0
a = [ 80 * n + ox, 160 * n + oy]
b = [160 * n + ox, 160 * n + oy]
c = [ 64 * n + ox, 135 * n + oy]
d = [120 * n + ox, 135 * n + oy]
e = [123 * n + ox, 135 * n + oy]
f = [160 * n + ox, 135 * n + oy]
g = [100 * n + ox, 20 * n + oy]
h = [120 * n + ox, 20 * n + oy]
c1 = Canvas(root, height = ch, width = cw)
for i in range(31):
y = 0 + 5 * i
c1.create_line(0, y, 200, y, fill = "cyan")
for i in range(5):
x0, y0 = a[0] - 2 * r * i, a[1] - r * i
x1, y1 = b[0] + 2 * r * i, b[1] - r * i
c1.create_line(x0, y0, x1, y1, fill = "orange")
print x0, y0, x1, y1
for i in range(loop_b):
x0, y0 = c[0] + xs * i, c[1] - s * i
x1, y1 = d[0] + 00 * i, d[1] - s * i
c1.create_line(x0, y0, x1, y1, fill = "white")
for i in range(loop_b):
x0, y0 = e[0] + 00 * i, e[1] - s * i
x1, y1 = f[0] - xr * i, f[1] - s * i
c1.create_line(x0, y0, x1, y1, fill = "white")
for j in range(loop_d):
for i in range(loop_c):
x0, y0 = 0.0 + 5 * i, 152.5 + 5 * j
x1, y1 = 2.5 + 5 * i, 150.0 + 5 * j
x2, y2 = 5.0 + 5 * i, 155.0 + 5 * j
c1.create_line(x0, y0, x1, y1, fill = "blue")
c1.create_line(x1, y1, x2, y2, fill = "blue")
c1.pack()
root.title("Lines")
root.mainloop()
</pre>
</blockquote>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9ZDbCvMTaq9TX2iXil7fp_jpVxVmc1DzYzgoIDLi3tobpKn4_DPg_-HEzQWgCR45svyDmcafWCJiEk7c4naeP9plENryO7At4X29hXlRc4x2yHg7BIJHtTCtZI0Bamrd7qkvXZkUuxGk/s1600/15_c_01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9ZDbCvMTaq9TX2iXil7fp_jpVxVmc1DzYzgoIDLi3tobpKn4_DPg_-HEzQWgCR45svyDmcafWCJiEk7c4naeP9plENryO7At4X29hXlRc4x2yHg7BIJHtTCtZI0Bamrd7qkvXZkUuxGk/s1600/15_c_01.png" /></a><br />
</body>
Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com0tag:blogger.com,1999:blog-3250011985483393306.post-4876658339893215322015-11-29T15:46:00.000-08:002015-12-02T15:37:33.514-08:00Drop Down Menus<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjq4Pl0ridl-DWnN2tawCz7Oh2GniTzwuhfBC43XC2vULsiMs1s6zWdX0uWA7Nvzwo6L6fVJlcPTsy2obMqg3GLAWbqSIAml6K32hcKlyEOjQcmzustMWZGTN6Gy8qzhnsUHwPnOi1lAUJG/s1600/14th_00101.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjq4Pl0ridl-DWnN2tawCz7Oh2GniTzwuhfBC43XC2vULsiMs1s6zWdX0uWA7Nvzwo6L6fVJlcPTsy2obMqg3GLAWbqSIAml6K32hcKlyEOjQcmzustMWZGTN6Gy8qzhnsUHwPnOi1lAUJG/s1600/14th_00101.png" /></a></div>
<br />
<br />
<span style="color: #bf9000;">The next code is a simple example of how to make appear menus with Tkinter:</span><br />
<br />
<blockquote class="tr_bq">
<span style="color: #6aa84f;">from </span>Tkinter<span style="color: #6aa84f;"> import *</span><br />
<span style="color: #6aa84f;">def</span> <span style="color: #e06666;">dns</span><span style="color: #6aa84f;">():</span><br />
<a name='more'></a><br />
<span style="color: #6aa84f;">pass</span><br />
root <span style="color: #6aa84f;">=</span> Tk<span style="color: #6aa84f;">()</span><br />
<br />
menu_01 <span style="color: #6aa84f;">=</span> Menu<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">)</span><br />
root<span style="color: #6aa84f;">.</span>config<span style="color: #6aa84f;">(</span>menu <span style="color: #6aa84f;">=</span> menu_01<span style="color: #6aa84f;">)</span><br />
<span style="color: #cccccc;"># -- Going inside another menu -- #</span><br />
sub_menu <span style="color: #6aa84f;">=</span> Menu<span style="color: #6aa84f;">(</span>menu_01<span style="color: #6aa84f;">)</span><br />
<span style="color: #bf9000;">""" Naming the Menu 01 """</span><br />
menu_01.add_cascade(label <span style="color: #6aa84f;">=</span> <span style="color: #f1c232;">"File"</span>, menu <span style="color: #6aa84f;">=</span> sub_menu)<br />
<span style="color: #cccccc;"># --- Adding elements to the menu_01</span><span style="color: #cccccc;"><span style="color: #cccccc;"> -- #</span> </span><br />
sub_menu.add_command(label <span style="color: #6aa84f;">=</span> <span style="color: #f1c232;">"New Project"</span>, command <span style="color: #6aa84f;">=</span> dns)<br />
sub_menu.add_command(label <span style="color: #6aa84f;">=</span><span style="color: #f1c232;"> "New ..."</span> , command <span style="color: #6aa84f;">=</span> dns)<br />
<span style="color: #bf9000;">""" Separator 01 - Menu 01 """</span><br />
sub_menu.add_separator()<br />
sub_menu.add_command(label<span style="color: #6aa84f;">=</span><span style="color: #f1c232;"> "Rooms"</span> , command <span style="color: #6aa84f;">=</span> dns)<br />
sub_menu.add_command(label <span style="color: #6aa84f;">=</span><span style="color: #f1c232;"> "Roost"</span> , command <span style="color: #6aa84f;">=</span> dns)<br />
""" Separator 02 - Menu 01 """<br />
sub_menu.add_separator()<br />
sub_menu.add_command(label <span style="color: #6aa84f;">=</span><span style="color: #f1c232;"> "Exit" </span> , command <span style="color: #6aa84f;">=</span> dns)<br />
""" Second Menu : Menu_02 """<br />
edit_menu <span style="color: #6aa84f;">=</span> Menu(menu_01)<br />
menu_01.add_cascade(label <span style="color: #6aa84f;">=</span><span style="color: #f1c232;"> "Edit"</span>, menu <span style="color: #6aa84f;">=</span> edit_menu)<br />
edit_menu.add_command(label <span style="color: #6aa84f;">=</span><span style="color: #f1c232;"> "Redo"</span>, command <span style="color: #6aa84f;">=</span> dns)<br />
<br />
<span style="color: #bf9000;">""" Third Menu : Menu_03 """</span><br />
risk_menu <span style="color: #6aa84f;">=</span> Menu(menu_01)<br />
menu_01<span style="color: #6aa84f;">.</span>add_cascade(label <span style="color: #6aa84f;">=</span> <span style="color: #f1c232;">"Crash"</span>, menu <span style="color: #6aa84f;">=</span> risk_menu)<br />
risk_menu<span style="color: #6aa84f;">.</span>add_command(label <span style="color: #6aa84f;">=</span><span style="color: #f1c232;"> "Break"</span>, command <span style="color: #6aa84f;">=</span> dns)<br />
root<span style="color: #6aa84f;">.</span>mainloop<span style="color: #6aa84f;">()</span></blockquote>
<div>
And, of course, the result when running the code should be something akin to:</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg88SBrzVnYmequ1FmQ8RhOzsSPv40mRZHvIn9LJV6YwU8v_AKbnw8JkjJN_lO7ZnAawG9G8xY0iPhxwWy3Hp5j2J1BkhduVNxGRz19dao0EuoIU2gKnmpE4OJ5oGXM36YJ227KR045E2E/s1600/14th_gif_01.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg88SBrzVnYmequ1FmQ8RhOzsSPv40mRZHvIn9LJV6YwU8v_AKbnw8JkjJN_lO7ZnAawG9G8xY0iPhxwWy3Hp5j2J1BkhduVNxGRz19dao0EuoIU2gKnmpE4OJ5oGXM36YJ227KR045E2E/s200/14th_gif_01.gif" width="168" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<br />
<br />Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com0tag:blogger.com,1999:blog-3250011985483393306.post-1019260062917096562015-10-26T15:19:00.001-07:002016-09-06T15:23:31.442-07:00Solving 2 Linear equations<head>
<style>
body {background-color:black}
h1 {color:blue}
p {color:green}
pre {color:white}
</style>
</head>
<body>
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2TMQf8LK6vQzMXh9SJ_CvZoWun_NtbcF7ry5EclP2yq1SGkotQq9lcWwrvQyJ8vRTNCmGBIbdXP8D_fewBKlHDnzSN7L6Gc_YFw9CMGrno079NA2JiPHZWfy2FoyGrQmCqSu7V4g9emo/s320/01_solve_2_LE_.png" alt="img_01" style="width:320px;height:140px">
<p>The next code is based on the Gauss Elimination method for solving a system of linear equations.</p>
<blockquote>
<pre>
# Gauss elimination method for 2 linear equations
import Tkinter as tk
def lab_1(txt, foreground, row, col):
w = tk.Label(root, text = txt, fg = foreground)
w.grid(row = row, column = col)
return w <a name='more'></a>
def lab_2(txt, row, col, cs):
w = tk.Label(root, text = txt)
w.grid(row = row, column = col, columnspan = cs)
return w
def lab_3(txt):
w = tk.Label(root, text = txt)
return w
def ent_1(txt_var, width_e, row, col):
w = tk.Entry(root, textvariable = txt_var, width = width_e)
w.grid(row = row, column = col)
return w
#tk.Button(root, text = "-> SOLVE", borderwidth = 4, command = solve_le)
def button(txt, border_width, cmd, row, col, rs, cs):
b = border_width
t = txt
w = tk.Button(root, text = t, borderwidth = b, command = cmd)
w.grid(row = row, column = col, rowspan = rs, columnspan = cs)
return w
def solve_le():
if a_val.get() == 0:
print "The value of 'a' must be greater than 0"
else:
a = a_val.get()
b = b_val.get()
c = c_val.get()
d = d_val.get()
e = e_val.get()
f = f_val.get()
nf = - d/a
d_n = nf * a + d
e_n = nf * b + e
f_n = nf * c + f
y = f_n / e_n
x = (c - b * y) / a
print "nf =", nf
x_result.config(text = x)
x_result.grid(row = 4, column = 3, sticky = tk.W, columnspan = 2)
y_result.config(text = y)
y_result.grid(row = 5, column = 3, sticky = tk.W, columnspan = 2)
""" Starting with Tkinter codes ... """
root = tk.Tk()
root.title("Solving 2 Linear Equations")
w_lbl = 3
w_ent = 19
b_txt = "-> SOLVE"
b_brw = 4
b_row = 4
b_col = 0
span1 = 2
span2 = 5
RED = "red"
BLUE = "blue"
a_val, b_val, c_val = tk.DoubleVar(), tk.DoubleVar(), tk.DoubleVar()
d_val, e_val, f_val = tk.DoubleVar(), tk.DoubleVar(), tk.DoubleVar()
eq_1_str = " A * x + B * y = C"
eq_2_str = " D * x + E * y = F"
lab_2(eq_1_str, 0, 0, span2)
lab_2(eq_2_str, 1, 0, span2)
""" <<<< Entries >>>> """
ent_1(a_val, w_ent, 2, 0)
ent_1(b_val, w_ent, 2, 2)
ent_1(c_val, w_ent, 2, 4)
ent_1(d_val, w_ent, 3, 0)
ent_1(e_val, w_ent, 3, 2)
ent_1(f_val, w_ent, 3, 4)
""" <<<< Labels >>>> """
lab_1(" x +" , RED, 2, 1)
lab_1(" y = ", RED, 2, 3)
lab_1(" x +" , BLUE, 3, 1)
lab_1(" y = ", BLUE, 3, 3)
lab_1(" x = ", BLUE, 4, 2)
lab_1(" y = ", BLUE, 5, 2)
x_result = lab_3(" ")
y_result = lab_3(" ")
""" <<<< Button >>>> """
button(b_txt, b_brw, solve_le, b_row, b_col, span1, span1)
root.mainloop()
</pre>
</blockquote>
<p><b>The results are going to be approximations because of the nature of the code, naturally.</b>
<br>
<br><b>Testing with the next pair of equations:</b>
<blockquote>
<pre>
3 * x1 + 2 * x2 = 18
(-1) * x1 + 2 * x2 = 2
</pre>
</blockquote>
<br>
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2TMQf8LK6vQzMXh9SJ_CvZoWun_NtbcF7ry5EclP2yq1SGkotQq9lcWwrvQyJ8vRTNCmGBIbdXP8D_fewBKlHDnzSN7L6Gc_YFw9CMGrno079NA2JiPHZWfy2FoyGrQmCqSu7V4g9emo/s320/01_solve_2_LE_.png" alt="img_02" style="width:320px;height:140px">
</body>Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com0tag:blogger.com,1999:blog-3250011985483393306.post-61868290317795992752015-05-11T14:31:00.000-07:002015-11-13T13:47:34.504-08:00Cross-multiplication Calculator (The Rule Of Three)<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixrmq8uEez77zlSR_w4IMn1ZdOk4KVyQ5eHgWaaEJ3q-IKIbzamXsIt3mV1UjL5rBczfOrE2WAmiwo8q6e06UaqHvf2hugmYfwvvjgCQGrXEllvztdygBGvvUMO3KdG0AfXgqAi1iOOME/s1600/12_01.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixrmq8uEez77zlSR_w4IMn1ZdOk4KVyQ5eHgWaaEJ3q-IKIbzamXsIt3mV1UjL5rBczfOrE2WAmiwo8q6e06UaqHvf2hugmYfwvvjgCQGrXEllvztdygBGvvUMO3KdG0AfXgqAi1iOOME/s1600/12_01.png" /></a></div>
In this post I will put a code tha makes a 'calculator' that uses cross-multiplication (or like the title says, <i>The Rule Of Three</i>).<br />
<br />
Well, (<i><a href="http://en.wikipedia.org/wiki/Cross-multiplication">Wikipedia link-here</a></i>) one can <b>cross-multiply</b> to determine the value of a variable.<br />
<br />
In this case it's like:<br />
<br />
<div style="text-align: center;">
<b>a <span style="color: #6aa84f;">* </span>x <span style="color: #cc0000;">=</span> b <span style="color: #6aa84f;">*</span> c</b></div>
Which ends up (for the 'x' variable) in:<br />
<br />
<div style="text-align: center;">
<b> x <span style="color: #cc0000;">=</span> c<span style="color: #6aa84f;"> *</span> b <span style="color: #6aa84f;">/</span> a</b></div>
<div style="text-align: center;">
</div>
<br />
<a name='more'></a>The code is the next one:<br />
<br />
<blockquote class="tr_bq">
## Cross-multiplication<br />
<span style="color: #6aa84f;">from </span>Tkinter <span style="color: #6aa84f;">import *</span><br />
<span style="color: #6aa84f;">import </span>tkMessageBox<br />
<br />
<span style="color: #6aa84f;">def </span><span style="color: #e06666;">button</span><span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> text<span style="color: #6aa84f;">,</span> width<span style="color: #6aa84f;">,</span> row<span style="color: #6aa84f;">,</span> column<span style="color: #6aa84f;">,</span> command<span style="color: #6aa84f;">=<b>None</b>,</span> cursor<span style="color: #6aa84f;">=<b>None</b>):</span><br />
btn <span style="color: #6aa84f;">=</span> Button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> text <span style="color: #6aa84f;">=</span> text<span style="color: #6aa84f;">,</span> command<span style="color: #6aa84f;">=</span>command<span style="color: #6aa84f;">,</span> cursor<span style="color: #6aa84f;">=</span>cursor<span style="color: #6aa84f;">,</span> bg <span style="color: #6aa84f;">=</span> <span style="color: #bf9000;">"black"</span><span style="color: #6aa84f;">,</span> fg <span style="color: #6aa84f;">=</span> <span style="color: #bf9000;">"yellow"</span><span style="color: #6aa84f;">,</span> width <span style="color: #6aa84f;">=</span> width<span style="color: #6aa84f;">)</span><br />
btn<span style="color: #6aa84f;">.</span>grid<span style="color: #6aa84f;">(</span>row<span style="color: #6aa84f;"> =</span> row<span style="color: #6aa84f;">,</span> column <span style="color: #6aa84f;">=</span> column<span style="color: #6aa84f;">)</span><br />
<span style="color: #6aa84f;">return </span>btn<br />
<br />
<span style="color: #6aa84f;">def </span><span style="color: #e06666;">entry</span><span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> textvar<span style="color: #6aa84f;">,</span> width<span style="color: #6aa84f;">,</span> row<span style="color: #6aa84f;">,</span> column<span style="color: #6aa84f;">)</span><span style="color: #6aa84f;">:</span><br />
ent <span style="color: #6aa84f;">=</span> Entry<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> textvariable <span style="color: #6aa84f;">=</span> textvar<span style="color: #6aa84f;">,</span> width <span style="color: #6aa84f;">=</span> width<span style="color: #6aa84f;">)</span><br />
ent<span style="color: #6aa84f;">.</span>grid<span style="color: #6aa84f;">(</span>row <span style="color: #6aa84f;">=</span> row<span style="color: #6aa84f;">,</span> column <span style="color: #6aa84f;">=</span> column<span style="color: #6aa84f;">)</span><br />
<span style="color: #6aa84f;">return </span>entry<br />
<br />
<span style="color: #6aa84f;">def </span><span style="color: #e06666;">pr35</span><span style="color: #6aa84f;">():</span><br />
<span style="color: #7f6000;"> '''<br /> a -> b<br /> c -> x<br /> x = c * b / a<br /> ''</span>'<br />
a <span style="color: #6aa84f;">=</span> a_val<span style="color: #6aa84f;">.</span>get<span style="color: #6aa84f;">()</span><br />
b <span style="color: #6aa84f;">=</span> b_val<span style="color: #6aa84f;">.</span>get<span style="color: #6aa84f;">()</span><br />
c <span style="color: #6aa84f;">=</span> c_val<span style="color: #6aa84f;">.</span>get<span style="color: #6aa84f;">()</span><br />
lab_x_val = Label<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> text <span style="color: #6aa84f;">=</span> " "<span style="color: #6aa84f;">,</span> bg <span style="color: #6aa84f;">=</span> "blue"<span style="color: #6aa84f;">,</span> fg <span style="color: #6aa84f;">=</span> "green"<span style="color: #6aa84f;">,</span> width <span style="color: #6aa84f;">=</span> entry_width<span style="color: #6aa84f;">)</span><br />
lab_x_val<span style="color: #6aa84f;">.</span>grid<span style="color: #6aa84f;">(</span>row <span style="color: #6aa84f;">=</span> 1<span style="color: #6aa84f;">,</span> column <span style="color: #6aa84f;">=</span> 3<span style="color: #6aa84f;">)</span><br />
<br />
<span style="color: #6aa84f;">if </span>a <span style="color: #6aa84f;">=</span><span style="color: #6aa84f;">=</span> 0<span style="color: #6aa84f;">:</span><br />
lab_x_val<span style="color: #6aa84f;">.</span>config<span style="color: #6aa84f;">(</span>text<span style="color: #6aa84f;">=</span><span style="color: #bf9000;">"a = 0"</span><span style="color: #6aa84f;">)</span><br />
<span style="color: #6aa84f;"> else:</span><br />
x <span style="color: #6aa84f;">=</span> <span style="color: #6aa84f;">str(</span>c <span style="color: #6aa84f;">*</span> b <span style="color: #6aa84f;">/ </span>a<span style="color: #6aa84f;">)</span><br />
lab_x_val<span style="color: #6aa84f;">.</span>config<span style="color: #6aa84f;">(</span>text = x<span style="color: #6aa84f;">)</span><br />
<br />
<span style="color: #6aa84f;">def </span><span style="color: #e06666;">info</span><span style="color: #6aa84f;">():</span><br />
a <span style="color: #6aa84f;">=</span><span style="color: #7f6000;"> '''</span><br />
<span style="color: #7f6000;"> All operations follow</span><br />
<span style="color: #7f6000;"> the next idea :</span><br />
<span style="color: #7f6000;"><br /></span>
<span style="color: #7f6000;"> a -> b</span><br />
<span style="color: #7f6000;"> c -> x</span><br />
<span style="color: #7f6000;"><br /></span>
<span style="color: #7f6000;"> x = c * b / a</span><br />
<span style="color: #7f6000;"> '''</span><br />
tkMessageBox<span style="color: #6aa84f;">.</span>showinfo<span style="color: #6aa84f;">(</span><span style="color: #bf9000;">"Information"</span><span style="color: #6aa84f;">,</span> a<span style="color: #6aa84f;">)</span><br />
<br />
<span style="color: #6aa84f;">def </span><span style="color: #e06666;">h3LP</span><span style="color: #6aa84f;">():</span><br />
a <span style="color: #6aa84f;">=</span><span style="color: #7f6000;"> '''</span><br />
<span style="color: #7f6000;"> The calculation that is</span><br />
<span style="color: #7f6000;"> being excuted is :</span><br />
<span style="color: #7f6000;"><br /></span>
<span style="color: #7f6000;"> x = c * b / a</span><br />
<span style="color: #7f6000;"><br /></span>
<span style="color: #7f6000;"> So the value of 'a' must</span><br />
<span style="color: #7f6000;"> be different from zero (0).</span><br />
<span style="color: #7f6000;"> '''</span><br />
tkMessageBox<span style="color: #6aa84f;">.</span>showinfo<span style="color: #6aa84f;">(</span><span style="color: #bf9000;">"Help"</span><span style="color: #6aa84f;">,</span> a<span style="color: #6aa84f;">)</span><br />
<br />
<span style="color: #6aa84f;">def </span><span style="color: #e06666;">cred</span><span style="color: #6aa84f;">():</span><br />
a <span style="color: #6aa84f;">=</span> <span style="color: #7f6000;">'''</span><br />
<span style="color: #7f6000;"> Jenko Stevens</span><br />
<span style="color: #7f6000;"><br /></span>
<span style="color: #7f6000;"> May 11th 2015</span><br />
<span style="color: #7f6000;"><br /></span>
<span style="color: #7f6000;"> Any questions about</span><br />
<span style="color: #7f6000;"> the source code, post it</span><br />
<span style="color: #7f6000;"> in the web page of:</span><br />
<span style="color: #7f6000;"><br /></span>
<span style="color: #7f6000;"> pytkstuff.blogspot.com</span><br />
<span style="color: #7f6000;"><br /></span>
<span style="color: #7f6000;"> Or send me and email to:</span><br />
<span style="color: #7f6000;"><br /></span>
<span style="color: #7f6000;"> mugendmonkey03@gmail.com</span><br />
<span style="color: #7f6000;"> '''</span><br />
tkMessageBox<span style="color: #6aa84f;">.</span>showinfo<span style="color: #6aa84f;">(</span><span style="color: #bf9000;">"Credits"</span><span style="color: #6aa84f;">,</span> a<span style="color: #6aa84f;">)</span><br />
<br />
root <span style="color: #6aa84f;">=</span> Tk<span style="color: #6aa84f;">()</span><br />
root<span style="color: #6aa84f;">.</span>title<span style="color: #6aa84f;">(</span><span style="color: #bf9000;">"Proporcionality"</span><span style="color: #6aa84f;">)</span><br />
<br />
entry_width <span style="color: #6aa84f;">=</span> 12<br />
label_width <span style="color: #6aa84f;">=</span> 6<br />
button_width <span style="color: #6aa84f;">=</span> 6<br />
<br />
a_val <span style="color: #6aa84f;">=</span> DoubleVar<span style="color: #6aa84f;">()</span><br />
b_val <span style="color: #6aa84f;">=</span> DoubleVar<span style="color: #6aa84f;">()</span><br />
c_val <span style="color: #6aa84f;">=</span> DoubleVar<span style="color: #6aa84f;">()</span><br />
<br />
# Just messing around<br />
pr35<span style="color: #6aa84f;">()</span><br />
<br />
bt_a <span style="color: #6aa84f;">=</span> button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span><span style="color: #bf9000;"> "a"</span><span style="color: #6aa84f;">,</span> label_width<span style="color: #6aa84f;">,</span> 0<span style="color: #6aa84f;">,</span> 0<span style="color: #6aa84f;">)</span><br />
bt_b <span style="color: #6aa84f;">=</span> button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> <span style="color: #bf9000;">"b"</span><span style="color: #6aa84f;">,</span> label_width<span style="color: #6aa84f;">, </span>0<span style="color: #6aa84f;">,</span> 2<span style="color: #6aa84f;">)</span><br />
bt_c <span style="color: #6aa84f;">=</span> button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> <span style="color: #bf9000;">"c"</span><span style="color: #6aa84f;">,</span> label_width<span style="color: #6aa84f;">,</span> 1<span style="color: #6aa84f;">,</span> 0<span style="color: #6aa84f;">)</span><br />
bt_x <span style="color: #6aa84f;">=</span> button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> <span style="color: #bf9000;">"x"</span><span style="color: #6aa84f;">,</span> label_width<span style="color: #6aa84f;">,</span> 1<span style="color: #6aa84f;">,</span> 2<span style="color: #6aa84f;">)</span><br />
<br />
ent1 <span style="color: #6aa84f;">=</span> entry<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> a_val<span style="color: #6aa84f;">,</span> entry_width<span style="color: #6aa84f;">,</span> 0<span style="color: #6aa84f;">,</span> 1<span style="color: #6aa84f;">)</span><br />
ent2 <span style="color: #6aa84f;">=</span> entry<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> b_val<span style="color: #6aa84f;">,</span> entry_width<span style="color: #6aa84f;">,</span> 0<span style="color: #6aa84f;">,</span> 3<span style="color: #6aa84f;">)</span><br />
ent3 <span style="color: #6aa84f;">=</span> entry<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> c_val<span style="color: #6aa84f;">,</span> entry_width<span style="color: #6aa84f;">,</span> 1<span style="color: #6aa84f;">,</span> 1<span style="color: #6aa84f;">)</span><br />
<br />
btnE <span style="color: #6aa84f;">=</span> button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> <span style="color: #bf9000;">"Exec"</span><span style="color: #6aa84f;">,</span> button_width<span style="color: #6aa84f;">,</span> 2<span style="color: #6aa84f;">,</span> 0<span style="color: #6aa84f;">,</span> pr35<span style="color: #6aa84f;">)</span><br />
btnH <span style="color: #6aa84f;">=</span> button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> <span style="color: #bf9000;">"??"</span> <span style="color: #6aa84f;">,</span> entry_width <span style="color: #6aa84f;">,</span> 2<span style="color: #6aa84f;">,</span> 1<span style="color: #6aa84f;">,</span> h3LP<span style="color: #6aa84f;">)</span><br />
btnI <span style="color: #6aa84f;">=</span> button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> <span style="color: #bf9000;">"Info"</span><span style="color: #6aa84f;">,</span> button_width<span style="color: #6aa84f;">,</span> 2<span style="color: #6aa84f;">,</span> 2<span style="color: #6aa84f;">,</span> info<span style="color: #6aa84f;">)</span><br />
btnC <span style="color: #6aa84f;">=</span> button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> <span style="color: #bf9000;">"!!" </span> <span style="color: #6aa84f;">,</span> entry_width <span style="color: #6aa84f;">,</span> 2<span style="color: #6aa84f;">,</span> 3<span style="color: #6aa84f;">,</span> cred<span style="color: #6aa84f;">)</span><br />
<br />
root<span style="color: #6aa84f;">.</span>mainloop<span style="color: #6aa84f;">()</span></blockquote>
In this case I've used the module tkMessageBox instead of creating customized pop-up windows. Some of the buttons in the bottom line are the ones that are linked to use message boxes, like:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6it-ImYreMi30N9kp596vmnl9cRb5ig4jOr5Iul2Lx9maBHxT1yKMuYhO_y_B7lDDspJr9b7JP1Uc8efQId2Kzhh1aCx9uDatscu6m5Cc9jKY086F8LMaouKIQOJbOv5RiLbVWhhV16Y/s1600/12_02.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="189" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6it-ImYreMi30N9kp596vmnl9cRb5ig4jOr5Iul2Lx9maBHxT1yKMuYhO_y_B7lDDspJr9b7JP1Uc8efQId2Kzhh1aCx9uDatscu6m5Cc9jKY086F8LMaouKIQOJbOv5RiLbVWhhV16Y/s320/12_02.png" width="320" /></a></div>
<br />
And some of the buttons doesn't have assigned commands, for now :P. Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com0tag:blogger.com,1999:blog-3250011985483393306.post-44999044142168884182015-05-07T17:42:00.002-07:002015-05-07T17:51:56.286-07:00Using tkMessageBox<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.java2s.com/Tutorial/Python/0360__Tkinker/OKCancelmessagebox.htm" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgNq0LCwy-67TLHqotXAHcnn88qnjG5USlUIpWFic-rM3zlB7umltxq6HE80Z2kJDAq8BInA_hVYDZdtEuOGCMjJJiAi_ZhCryI0u00hGDnkhHwoETJZUaYI5MQy2aUFBRsBoBJMRSOTjY/s1600/OKCancelmessagebox.PNG" /></a></div>
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. <br />
<br />
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 : <a href="http://www.tutorialspoint.com/python/tk_messagebox.htm">Python Tkinter tkMessageBox</a>; or <a href="http://www.java2s.com/Tutorial/Python/0360__Tkinker/OKCancelmessagebox.htm">this other one for the code of the message box of the first image in this post</a>)<br />
<br />
Anyway, this is the simple code (a warning will be shown when the user clicks the button that says "Warning"):<br />
<br />
<blockquote class="tr_bq">
<span style="color: #6aa84f;">import </span>Tkinter <span style="color: #6aa84f;">as </span>tk<br />
<span style="color: #6aa84f;">import </span>tkMessageBox<br />
<br />
root <span style="color: #6aa84f;">= </span>tk<span style="color: #6aa84f;">.</span>Tk<span style="color: #6aa84f;">()</span><br />
<br />
<span style="color: #6aa84f;">def </span><span style="color: #e06666;">info</span><span style="color: #6aa84f;">():</span><br />
tkMessageBox<span style="color: #6aa84f;">.</span>showwarning<span style="color: #6aa84f;">(</span><span style="color: #f1c232;">"Warning"</span><span style="color: #6aa84f;">,</span> <span style="color: #f1c232;">"Resources changed"</span><span style="color: #6aa84f;">)</span><br />
<br />
tk<span style="color: #6aa84f;">.</span>Button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> text <span style="color: #6aa84f;">=</span> <span style="color: #f1c232;">"Pop up"</span><span style="color: #6aa84f;">,</span> command <span style="color: #6aa84f;">=</span> info<span style="color: #6aa84f;">)</span><span style="color: #6aa84f;">.</span>pack<span style="color: #6aa84f;">()</span><br />
<br />
root<span style="color: #6aa84f;">.</span>mainloop<span style="color: #6aa84f;">()</span></blockquote>
<br />
<a name='more'></a>This is how it looks:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOkJzR3Imhxu5XvIS3XYF-POa9gjQjx2FBEb-lSv2W_9XRlCu5wRHhQzMNoJFFkK1gWXjnAS9a1E0G0Fwdd5W3vw3OGGr16n1tudd2J8FVXWgwSgBQuLQHhutqWK7tcUgZRJu7SO9IDKI/s1600/11_01.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="108" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOkJzR3Imhxu5XvIS3XYF-POa9gjQjx2FBEb-lSv2W_9XRlCu5wRHhQzMNoJFFkK1gWXjnAS9a1E0G0Fwdd5W3vw3OGGr16n1tudd2J8FVXWgwSgBQuLQHhutqWK7tcUgZRJu7SO9IDKI/s200/11_01.JPG" width="200" /></a></div>
<br />
The second code is a modification of a previous example (<a href="http://pytkstuff.blogspot.com/2015/04/creating-simple-reynolds-number.html">_!!!</a>_), in this case the display box is for showing information.<br />
<br />
Of course, this is the image to be used:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0Uzoy0Op-OjbEd5URsrvG9Kr06k6_9t-Tydvz4-GrpKU_cv8OzyBvqykaiiQD-7DZaMgaNkYdiuOqQmaj9B5Sef6UR7drxe7N_sXdFiJV8Jinb81AjUaAg6IyWUvNmp2lNf60PFtHyow/s1600/_Re_01_54x25.GIF" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0Uzoy0Op-OjbEd5URsrvG9Kr06k6_9t-Tydvz4-GrpKU_cv8OzyBvqykaiiQD-7DZaMgaNkYdiuOqQmaj9B5Sef6UR7drxe7N_sXdFiJV8Jinb81AjUaAg6IyWUvNmp2lNf60PFtHyow/s1600/_Re_01_54x25.GIF" /></a></div>
And this is the code:<br />
<br />
<blockquote class="tr_bq">
<span style="color: #6aa84f;">from </span>Tkinter<span style="color: #6aa84f;"> import *</span><br />
<span style="color: #6aa84f;">import </span>tkMessageBox<br />
<br />
<span style="color: #6aa84f;">def </span><span style="color: #e06666;">calc_Re</span><span style="color: #6aa84f;">():</span><br />
<span style="color: #bf9000;"> """<br /> Re : Reynolds Number<br /> (Adimensional number used to determine<br /> if a fluid flow is laminar or turbulent type)<br /> Re = velocity * diameter / kinematic_viscosity<br /> Re = v__val * d__val / nu_val<br /> (row = 3, column = 1)<br /> """</span><br />
<span style="color: #6aa84f;">if </span>nu_val<span style="color: #6aa84f;">.</span>get() <span style="color: #6aa84f;">==</span> 0<span style="color: #6aa84f;">:</span><br />
print "Kinematic viscosity must be greater than zero"<br />
lab4<span style="color: #6aa84f;">.</span>config(text<span style="color: #6aa84f;">=</span>"Nu = 0")<br />
lab4<span style="color: #6aa84f;">.</span>grid(row <span style="color: #6aa84f;">=</span> 3, column <span style="color: #6aa84f;">=</span>1)<br />
<br />
<span style="color: #6aa84f;">else:</span><br />
v_ <span style="color: #6aa84f;">=</span> v__val<span style="color: #6aa84f;">.</span>get<span style="color: #6aa84f;">()</span><br />
d_ <span style="color: #6aa84f;">=</span> d__val<span style="color: #6aa84f;">.</span>get<span style="color: #6aa84f;">()</span><br />
nu <span style="color: #6aa84f;">=</span> nu_val<span style="color: #6aa84f;">.</span>get<span style="color: #6aa84f;">()</span><br />
re_<span style="color: #6aa84f;">=</span> v_ * d_ / nu<br />
result <span style="color: #6aa84f;">=</span> "Re = " + str(re_)<br />
print <span style="color: #f1c232;">"Re = {: f};(U = {: f}, D = {: f}, v = {: f})"</span><span style="color: #6aa84f;">.</span>format(re_, v_, d_, nu)<br />
lab4<span style="color: #6aa84f;">.</span>config(text<span style="color: #6aa84f;">=</span>result)<br />
lab4<span style="color: #6aa84f;">.</span>grid(row <span style="color: #6aa84f;">=</span> 3, column <span style="color: #6aa84f;">=</span> 1)<br />
<br />
<span style="color: #6aa84f;">def </span><span style="color: #e06666;">info</span><span style="color: #6aa84f;">():</span><br />
tkMessageBox.showinfo("Information", "Re: Reynolds number in a pipeline")<br />
<br />
root <span style="color: #6aa84f;">=</span> Tk()<br />
root.title("Reynolds (Re)")<br />
<br />
img_01 <span style="color: #6aa84f;">= </span>PhotoImage(file <span style="color: #6aa84f;">=</span> <span style="color: #f1c232;">"_Re_01_54x25.gif"</span>)<br />
v__val <span style="color: #6aa84f;">=</span> DoubleVar<span style="color: #6aa84f;">()</span><br />
d__val <span style="color: #6aa84f;">=</span> DoubleVar<span style="color: #6aa84f;">()</span><br />
nu_val <span style="color: #6aa84f;">=</span> DoubleVar<span style="color: #6aa84f;">()</span><br />
<br />
lab1 <span style="color: #6aa84f;">=</span> Label(root, text = "velo: U =")<span style="color: #6aa84f;">.</span>grid(row <span style="color: #6aa84f;">=</span> 0, column <span style="color: #6aa84f;">=</span> 0)<br />
lab2 <span style="color: #6aa84f;">=</span> Label(root, text = "diam: D =")<span style="color: #6aa84f;">.</span>grid(row <span style="color: #6aa84f;">=</span> 1, column <span style="color: #6aa84f;">=</span> 0)<br />
lab3 <span style="color: #6aa84f;">=</span> Label(root, text = "nu__: v =")<span style="color: #6aa84f;">.</span>grid(row <span style="color: #6aa84f;">=</span> 2, column <span style="color: #6aa84f;">=</span> 0)<br />
lab4 <span style="color: #6aa84f;">=</span> Label(root, text = " ")<br />
<br />
ent1 <span style="color: #6aa84f;">=</span> Entry(root, textvariable <span style="color: #6aa84f;">=</span> v__val)<span style="color: #6aa84f;">.</span>grid(row <span style="color: #6aa84f;">=</span> 0, column <span style="color: #6aa84f;">=</span> 1)<br />
ent2 <span style="color: #6aa84f;">=</span> Entry(root, textvariable <span style="color: #6aa84f;">=</span> d__val)<span style="color: #6aa84f;">.</span>grid(row <span style="color: #6aa84f;">=</span> 1, column <span style="color: #6aa84f;">=</span> 1)<br />
ent3 <span style="color: #6aa84f;">=</span> Entry(root, textvariable <span style="color: #6aa84f;">=</span> nu_val)<span style="color: #6aa84f;">.</span>grid(row <span style="color: #6aa84f;">=</span> 2, column <span style="color: #6aa84f;">=</span> 1)<br />
<br />
btn1 <span style="color: #6aa84f;">=</span> Button(root, text <span style="color: #6aa84f;">=</span> "Info" , command <span style="color: #6aa84f;">=</span> info, bg <span style="color: #6aa84f;">=</span> "black", fg <span style="color: #6aa84f;">=</span> "yellow")<span style="color: #6aa84f;">.</span>grid(row <span style="color: #6aa84f;">=</span> 2, column <span style="color: #6aa84f;">=</span> 2)<br />
btn2 <span style="color: #6aa84f;">=</span> Button(root, image <span style="color: #6aa84f;">=</span> img_01, command <span style="color: #6aa84f;">=</span> calc_Re)<span style="color: #6aa84f;">.</span>grid(row <span style="color: #6aa84f;">=</span> 3, column <span style="color: #6aa84f;">=</span> 0)<br />
<br />
root<span style="color: #6aa84f;">.</span>mainloop<span style="color: #6aa84f;">()</span></blockquote>
The modifications were the implementation of the "info" function and the button with the same name, with changes in its colors.<br />
This is the result if the button is clicked:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZJ2hcxm1VnXlYBNPTWTB1aR7kTRuqQXySDpBugAJgKZQ2G1CggjZYN0LUtDG-gafSM8H9rugsZ48wfsAFgVFC16Fy836_CkRc3aF96BUdNJvfpA7KyF-UIrem5uUl4x9Pcbg6uLjpkIc/s1600/11_02.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgZJ2hcxm1VnXlYBNPTWTB1aR7kTRuqQXySDpBugAJgKZQ2G1CggjZYN0LUtDG-gafSM8H9rugsZ48wfsAFgVFC16Fy836_CkRc3aF96BUdNJvfpA7KyF-UIrem5uUl4x9Pcbg6uLjpkIc/s1600/11_02.JPG" /></a></div>
Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com0tag:blogger.com,1999:blog-3250011985483393306.post-46781408461783121382015-04-25T18:01:00.002-07:002015-06-04T18:53:35.517-07:00Creating a simple Reynolds number calculatorThis time I'm posting a simple Reynolds number calculator, it has been done without using the class mechanism because I was getting some wierd results.<br />
<br />
The image that is going to be used is the next one:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7Y6tqgZ_r1d7Ks2j8hn3ZsBruZ2LBtuy9PjUxNigjewlZkLy_-WBtFnTRx9tE2H8nU9GIuV-c8TgJMkKCU_gQg0oKFvhZY8lemz5kZztt34MpcRwX-iWvJXCDmMeuhaAJ4zOr4rA7tcw/s1600/_Re_01_54x25.GIF" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj7Y6tqgZ_r1d7Ks2j8hn3ZsBruZ2LBtuy9PjUxNigjewlZkLy_-WBtFnTRx9tE2H8nU9GIuV-c8TgJMkKCU_gQg0oKFvhZY8lemz5kZztt34MpcRwX-iWvJXCDmMeuhaAJ4zOr4rA7tcw/s1600/_Re_01_54x25.GIF" /></a></div>
The code is the next one:<br />
<br />
<blockquote class="tr_bq">
<span style="color: #6aa84f;">from</span> Tkinter import <span style="color: #6aa84f;">*</span><br />
<br />
<span style="color: #6aa84f;">def</span> <span style="color: #ea9999;">calc_Re</span><span style="color: #6aa84f;">():</span><br />
<span style="color: #bf9000;">"""</span><br />
<span style="color: #bf9000;"> Re : Reynolds Number</span><br />
<span style="color: #bf9000;"> (Adimensional number used to determine</span><br />
<span style="color: #bf9000;"> if a fluid flow is laminar or turbulent type)</span><br />
<span style="color: #bf9000;"> Re = velocity * diameter / kinematic_viscosity</span><br />
<span style="color: #bf9000;"> Re = v__val * d__val / nu_val</span><br />
<span style="color: #bf9000;"> (row = 3, column = 1)</span><br />
<span style="color: #bf9000;"> """</span><br />
<span style="color: #6aa84f;"> if </span>nu_val<span style="color: #6aa84f;">.</span>get<span style="color: #6aa84f;">() ==</span> <b><span style="color: #6fa8dc;">0</span></b><span style="color: #6aa84f;">:</span><br />
print <span style="color: #f1c232;">"Kinematic viscosity must be greater than zero"</span><br />
lab4<span style="color: #6aa84f;">.</span>config<span style="color: #ea9999;"></span><span style="color: #6aa84f;">(</span>text<span style="color: #6aa84f;">=</span><span style="color: #f1c232;">"Nu = 0"</span><span style="color: #6aa84f;">)</span><br />
<a name='more'></a><br />
lab4<span style="color: #6aa84f;">.</span>grid<span style="color: #ea9999;"></span><span style="color: #6aa84f;">(</span>row <span style="color: #6aa84f;">=</span> 3<span style="color: #6aa84f;">,</span> column <span style="color: #6aa84f;">=</span> 1<span style="color: #6aa84f;">)</span><br />
<br />
<span style="color: #6aa84f;">else:</span><br />
v_ <span style="color: #6aa84f;">=</span> v__val<span style="color: #6aa84f;">.</span>get<span style="color: #ea9999;"></span><span style="color: #6aa84f;">()</span><br />
d_ <span style="color: #6aa84f;">=</span> d__val<span style="color: #6aa84f;">.</span>get<span style="color: #ea9999;"></span><span style="color: #6aa84f;">()</span><br />
nu <span style="color: #6aa84f;">=</span> nu_val<span style="color: #6aa84f;">.</span>get<span style="color: #ea9999;"></span><span style="color: #6aa84f;">()</span><br />
re_<span style="color: #6aa84f;">=</span> v_ * d_ / nu<br />
result <span style="color: #6aa84f;">=</span> <span style="color: #f1c232;">"Re = "</span> <span style="color: #6aa84f;">+</span> <span style="color: #93c47d;">str</span><span style="color: #6aa84f;">(</span>re_<span style="color: #6aa84f;">)</span><br />
print <span style="color: #f1c232;">"Re = {: f};(U = {: f}, D = {: f}, v = {: f})"</span><span style="color: #6aa84f;">.</span>format<span style="color: #6aa84f;">(</span>re_<span style="color: #6aa84f;">,</span> v_<span style="color: #6aa84f;">,</span> d_<span style="color: #6aa84f;">,</span> nu<span style="color: #6aa84f;">)</span><br />
lab4.config<span style="color: #6aa84f;">(</span>text<span style="color: #6aa84f;">=</span>result<span style="color: #6aa84f;">)</span><br />
lab4.grid<span style="color: #6aa84f;">(</span>row <span style="color: #6aa84f;">=</span> 3<span style="color: #6aa84f;">,</span> column <span style="color: #6aa84f;">=</span> 1<span style="color: #6aa84f;">)</span><br />
<br />
<br />
root <span style="color: #6aa84f;">=</span> Tk<span style="color: #6aa84f;">()</span><br />
root.title<span style="color: #6aa84f;">(</span><span style="color: #f1c232;">"Reynolds (Re)"</span><span style="color: #6aa84f;">)</span><br />
<br />
img_01 <span style="color: #6aa84f;">=</span> PhotoImage<span style="color: #6aa84f;">(</span>file <span style="color: #6aa84f;">=</span> <span style="color: #f1c232;">"_Re_01_54x25.gif"</span><span style="color: #6aa84f;">)</span><br />
v__val <span style="color: #6aa84f;">=</span> DoubleVar<span style="color: #6aa84f;">()</span><br />
d__val <span style="color: #6aa84f;">=</span> DoubleVar<span style="color: #6aa84f;">()</span><br />
nu_val <span style="color: #6aa84f;">=</span> DoubleVar<span style="color: #6aa84f;">()</span><br />
<br />
lab1 <span style="color: #6aa84f;">=</span> Label<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> text <span style="color: #6aa84f;">=</span> "velo: U ="<span style="color: #6aa84f;">)</span><span style="color: #6aa84f;">.</span>grid<span style="color: #6aa84f;">(</span>row <span style="color: #6aa84f;">=</span> 0<span style="color: #6aa84f;">,</span> column <span style="color: #6aa84f;">=</span> 0<span style="color: #6aa84f;">)</span><br />
lab2 <span style="color: #6aa84f;">=</span> Label<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> text <span style="color: #6aa84f;">=</span> "diam: D ="<span style="color: #6aa84f;">)</span><span style="color: #6aa84f;">.</span>grid<span style="color: #6aa84f;">(</span>row <span style="color: #6aa84f;">=</span> 1<span style="color: #6aa84f;">,</span> column <span style="color: #6aa84f;">=</span> 0<span style="color: #6aa84f;">)</span><br />
lab3 <span style="color: #6aa84f;">=</span> Label<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> text <span style="color: #6aa84f;">=</span> "nu__: v ="<span style="color: #6aa84f;">)</span><span style="color: #6aa84f;">.</span>grid<span style="color: #6aa84f;">(</span>row <span style="color: #6aa84f;">=</span> 2<span style="color: #6aa84f;">,</span> column <span style="color: #6aa84f;">=</span> 0<span style="color: #6aa84f;">)</span><br />
lab4 <span style="color: #6aa84f;">=</span> Label<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> text <span style="color: #6aa84f;">=</span> " "<span style="color: #6aa84f;">)</span><br />
<br />
ent1 <span style="color: #6aa84f;">=</span> Entry<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> textvariable <span style="color: #6aa84f;">=</span> v__val<span style="color: #6aa84f;">)</span><span style="color: #6aa84f;">.</span>grid<span style="color: #6aa84f;">(</span>row <span style="color: #6aa84f;">=</span> 0<span style="color: #6aa84f;">,</span> column <span style="color: #6aa84f;">=</span> 1<span style="color: #6aa84f;">)</span><br />
ent2 <span style="color: #6aa84f;">=</span> Entry<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> textvariable <span style="color: #6aa84f;">=</span> d__val<span style="color: #6aa84f;">)</span><span style="color: #6aa84f;">.</span>grid<span style="color: #6aa84f;">(</span>row <span style="color: #6aa84f;">=</span> 1<span style="color: #6aa84f;">,</span> column <span style="color: #6aa84f;">=</span> 1<span style="color: #6aa84f;">)</span><br />
ent3 <span style="color: #6aa84f;">=</span> Entry<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> textvariable <span style="color: #6aa84f;">=</span> nu_val<span style="color: #6aa84f;">)</span><span style="color: #6aa84f;">.</span>grid<span style="color: #6aa84f;">(</span>row <span style="color: #6aa84f;">=</span> 2<span style="color: #6aa84f;">,</span> column <span style="color: #6aa84f;">=</span> 1<span style="color: #6aa84f;">)</span><br />
<br />
btn1 <span style="color: #6aa84f;">=</span> Button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> image <span style="color: #6aa84f;">=</span> img_01<span style="color: #6aa84f;">,</span> command <span style="color: #6aa84f;">=</span> calc_Re<span style="color: #6aa84f;">).</span>grid<span style="color: #6aa84f;">(</span>row <span style="color: #6aa84f;">=</span> 3<span style="color: #6aa84f;">,</span> column <span style="color: #6aa84f;">=</span> 0<span style="color: #6aa84f;">)</span><br />
<br />
root<span style="color: #6aa84f;">.</span>mainloop<span style="color: #6aa84f;">()</span></blockquote>
<br />
<br />
As you can see the "grid" system is used for the organization of everything.<br />
<br />
The result for when you run the code is:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDwulS8MEI1rC-nWTV-Q8WiVNHiyo-xTgQtT_LZOcUaGkEKllwilUUVzNfvvXKOIGgA-wuBbh_RfVKHV-xFGNBkFqUuLlyLpKlVmdHdQ6BOBfC0LDXGy48jXs9YguIcwun1nOPd8f0p04/s1600/10_01_img.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDwulS8MEI1rC-nWTV-Q8WiVNHiyo-xTgQtT_LZOcUaGkEKllwilUUVzNfvvXKOIGgA-wuBbh_RfVKHV-xFGNBkFqUuLlyLpKlVmdHdQ6BOBfC0LDXGy48jXs9YguIcwun1nOPd8f0p04/s1600/10_01_img.JPG" /></a></div>
<br />
You can try the consistence of the definitions by seeing that the zero division is being controlled, the message in that situation would be "Nu = 0":<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgULVglsejlQSCucueRd0egfZDOUnPU7m80RMeZKvFuSo4vxVnvCq0qZH5RJXSRpgmkRMmO2VAt0MeAmyMAH6jh6bbp-13AMVXRi3GqAWFJNzSxSVR0aG6TpT1g5b7rE_CJiW1IrDJAius/s1600/10_02_img.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgULVglsejlQSCucueRd0egfZDOUnPU7m80RMeZKvFuSo4vxVnvCq0qZH5RJXSRpgmkRMmO2VAt0MeAmyMAH6jh6bbp-13AMVXRi3GqAWFJNzSxSVR0aG6TpT1g5b7rE_CJiW1IrDJAius/s1600/10_02_img.JPG" /></a></div>
<br />
<br />
The rest is just a question of using valid values and see the results:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUANAISL9Oj5V1v0lyp34VEWJBTAbDOQVLSMWCD1z8D6ac2rEtOaez26hltQ3DtTXSzRizBr41k2yD-ZFAWivETTAbm9T0HIMpPSWqRUsp7HJg2e2rku16fxXUgLAMcrZFxd3WEYrt3ws/s1600/10_03_img.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUANAISL9Oj5V1v0lyp34VEWJBTAbDOQVLSMWCD1z8D6ac2rEtOaez26hltQ3DtTXSzRizBr41k2yD-ZFAWivETTAbm9T0HIMpPSWqRUsp7HJg2e2rku16fxXUgLAMcrZFxd3WEYrt3ws/s1600/10_03_img.JPG" /></a></div>
<br />Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com0tag:blogger.com,1999:blog-3250011985483393306.post-30982923857733472132015-04-23T17:09:00.000-07:002016-11-22T10:02:23.593-08:00Cursors in Python TkinterNow it's time to use one of the stuff that support Python TKinter: mouse cursor.<br />
<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5FxtTX6nny1brvAj6XW4ZKsmEOiAoFoO0DlPhWHvXskLcjzXrByLGCOm6a-inpSYu_Jiv2mDeeh_jyMjHyVmoHxaRJQ-VPX3p5gJNqz-6WWuACb9d6oEkDY-DUjNXmcCrFGKRe6vWCTU/s1600/python_tkinter_cursors.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5FxtTX6nny1brvAj6XW4ZKsmEOiAoFoO0DlPhWHvXskLcjzXrByLGCOm6a-inpSYu_Jiv2mDeeh_jyMjHyVmoHxaRJQ-VPX3p5gJNqz-6WWuACb9d6oEkDY-DUjNXmcCrFGKRe6vWCTU/s1600/python_tkinter_cursors.png" height="201" width="320"></a></div>
<br />
<br />
The simple way to show how this works is shown with the next code (used in a previous post, with a little modification):<br />
<br />
<blockquote class="tr_bq">
<span style="color: #6aa84f;">import </span>Tkinter<br />
root <span style="color: #6aa84f;">=</span> Tkinter<span style="color: #6aa84f;">.</span>Tk<span style="color: #6aa84f;">()</span><br />
button <span style="color: #6aa84f;">=</span> Tkinter.Button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> text <span style="color: #6aa84f;">=</span> <span style="color: #bf9000;">"Forgotten Sign"</span><span style="color: #6aa84f;">, </span>cursor<span style="color: #6aa84f;">=</span><span style="color: #bf9000;">"rtl_logo"</span><span style="color: #6aa84f;">).</span>pack<span style="color: #6aa84f;">()</span><br />
root<span style="color: #6aa84f;">.</span>mainloop<span style="color: #6aa84f;">()</span></blockquote>
<br />
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOOH5D5H_4-BYhPdwtJ_KUFVm1Sc8yPTiP3wfzxuOaEhCY9IgXFtLz1NueMeUgYLiZW_CV_Z8ZpsdVVBSfawfO81hb-TE0INujU0FqUdZp4osPHinuvOLu5ECEwLAo2UYx5bxShw2dnSY/s1600/cursor_test_tk_0101.gif" alt="img_cursor_test_tk_0101" style="width:658px;height:259px">
<br />
<br />
The next code is a modification of a previous posted code that uses the class mechanism:<br />
<br />
<blockquote class="tr_bq">
<span style="color: #6aa84f;">from </span>Tkinter <span style="color: #6aa84f;">import *</span></blockquote>
<br />
<blockquote class="tr_bq">
<span style="color: #6aa84f;">def </span><span style="color: #e06666;">frame</span>(root, side):<br />
<span style="color: #999999;"># wf : Window Frame</span><br />
wf <span style="color: #6aa84f;">= </span>Frame<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">)</span><br />
wf<span style="color: #6aa84f;">.</span>pack(side<span style="color: #6aa84f;">=</span>side, expand<span style="color: #6aa84f;">=</span>YES<span style="color: #6aa84f;">,</span> fill<span style="color: #6aa84f;">=</span>BOTH<span style="color: #6aa84f;">)</span><br />
<span style="color: #6aa84f;">return </span>wf<br />
<br />
<span style="color: #6aa84f;">def </span><span style="color: #e06666;">button</span><span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> side<span style="color: #6aa84f;">,</span> text<span style="color: #6aa84f;">,</span> command<span style="color: #6aa84f;">=None,</span> cursor<span style="color: #6aa84f;">=None</span><span style="color: #6aa84f;">):</span><br />
btn <span style="color: #6aa84f;">= </span>Button<span style="color: #6aa84f;">(</span>root, text<span style="color: #6aa84f;">=</span>text<span style="color: #6aa84f;">, </span>command<span style="color: #6aa84f;">=</span>command<span style="color: #6aa84f;">,</span> cursor<span style="color: #6aa84f;">=</span>cursor<span style="color: #6aa84f;">)</span><br />
btn<span style="color: #6aa84f;">.</span>pack<span style="color: #6aa84f;">(</span>side<span style="color: #6aa84f;">=</span>side<span style="color: #6aa84f;">,</span> expand<span style="color: #6aa84f;">=</span>YES<span style="color: #6aa84f;">,</span> fill<span style="color: #6aa84f;">=</span>BOTH<span style="color: #6aa84f;">)</span><br />
<span style="color: #6aa84f;">return </span>btn<br />
<br />
<span style="color: #6aa84f;">class </span><b><span style="color: #e06666;">Sample</span></b><span style="color: #6aa84f;">(</span>Frame<span style="color: #6aa84f;">):</span><br />
<span style="color: #6aa84f;">def </span><span style="color: #e06666;">__init__</span><span style="color: #6aa84f;">(self):</span><br />
Frame<span style="color: #6aa84f;">.</span><b><span style="color: #93c47d;">__init__</span></b><span style="color: #6aa84f;">(self)</span><br />
<span style="color: #6aa84f;">self.</span>pack<span style="color: #6aa84f;">(</span>expand<span style="color: #6aa84f;">=</span>YES<span style="color: #6aa84f;">,</span> fill<span style="color: #6aa84f;">=</span>BOTH<span style="color: #6aa84f;">)</span><br />
<span style="color: #6aa84f;">self.</span>master.title<span style="color: #6aa84f;">(</span><span style="color: #bf9000;">"Simpl3"</span><span style="color: #6aa84f;">)</span><br />
<span style="color: #6aa84f;">self.</span>master.iconname<span style="color: #6aa84f;">(</span><span style="color: #6aa84f;"></span><span style="color: #bf9000;">"Simpl3"</span><span style="color: #6aa84f;">)</span><br />
<br />
opsF <span style="color: #6aa84f;">= </span>frame<span style="color: #6aa84f;">(self,</span> TOP<span style="color: #bf9000;"></span><span style="color: #6aa84f;">)</span><br />
btn0 <span style="color: #6aa84f;">= </span>button<span style="color: #6aa84f;">(</span>opsF<span style="color: #6aa84f;">,</span> LEFT<span style="color: #6aa84f;">,</span> <span style="color: #bf9000;">"About time!"</span><span style="color: #6aa84f;">,</span> cursor<span style="color: #6aa84f;">=</span><span style="color: #bf9000;">"pirate"</span><span style="color: #6aa84f;">)</span><br />
btn1 <span style="color: #6aa84f;">= </span>button<span style="color: #6aa84f;">(</span>opsF<span style="color: #6aa84f;">,</span> LEFT<span style="color: #6aa84f;">,</span> <span style="color: #bf9000;">"Pattern"</span><span style="color: #6aa84f;">,</span> cursor<span style="color: #6aa84f;">=</span><span style="color: #bf9000;">"rtl_logo"</span><span style="color: #6aa84f;">)</span><br />
btn2 <span style="color: #6aa84f;">=</span> button<span style="color: #6aa84f;">(</span>opsF<span style="color: #6aa84f;">,</span> LEFT<span style="color: #6aa84f;">,</span><span style="color: #bf9000;"> "Preloading..."</span><span style="color: #6aa84f;">,</span> cursor<span style="color: #6aa84f;">=</span><span style="color: #bf9000;">"watch"</span><span style="color: #6aa84f;">)</span><br />
<br />
<span style="color: #6aa84f;">if</span> <b><span style="color: lime;">__name__</span></b> <span style="color: #6aa84f;">== </span><span style="color: #bf9000;">"__main__"</span><span style="color: #6aa84f;">:</span><br />
Sample<span style="color: #6aa84f;">().</span>mainloop<span style="color: #6aa84f;">()</span></blockquote>
<br />
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjl5ZIlTp949pI0JlXHV4h8bTbM7AQgV-hh3JAt-MsMzidtglQNaK2PBjpQJkv5FCwsvpOwxQ14r-4ZLygoV0OdDXa7A5_OgQB4y1DB8-aDklOLb3ugMYwUsiGNiKfzUOLTDvOmskKPeQw/s1600/cursor_test_tk_0202.gif" alt="img_cursor_test_tk_0202" style="width:525px;height:295px">
<br />
<br />
Well, there has been some troubles at taking shots from the frickin' cursor, this post was with the lonely initial picture, but now that's been fixed :P<br />
<br />
The next list mention a few of them:<br />
-arrow<br />
-circle<br />
-clock<br />
-cross<br />
-dotbox<br />
-exchange<br />
-fleur<br />
-hand1<br />
-hand2<br />
-heart<br />
-man<br />
-pirate<br />
-plus<br />
-sailboat<br />
-star<br />
-target<br />
-trek<br />
-watch<br />
-xterm<br />
<br />
In the next links you can find a list of cursors:<br />
<a href="http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/cursors.html"><span style="color: #cc0000;">§</span> http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/cursors.html</a><br />
<br />
<a href="http://www.tcl.tk/man/tcl8.4/TkCmd/cursors.htm"><span style="color: #cc0000;">§</span> http://www.tcl.tk/man/tcl8.4/TkCmd/cursors.htm</a>Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com3tag:blogger.com,1999:blog-3250011985483393306.post-43559316922444048782015-04-20T18:33:00.002-07:002015-04-20T18:34:04.948-07:00Using images with TKinterNow is time that we use images for our oncoming projects, first I will start with a simple piece of code, but before that you must make sure to save the script in the same folder in which the images are contained (well, at least with the present code), this images are named "8_01.gif", "8_02.gif", "8_03.gif", "8_04.gif", "8_05.gif":<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJ4-Jb4RRABunNBLw3mg24ekd5A-9nHhob2TWA0c8ofZSfGD4fyqMtVArTORzqRw1BSgaF2fDkp3v5_K1Y20XnnK35oEG1-_T5TsL7gPx3t9kh12pbxEqW481m9yOaq7T500x2-jg5cNE/s1600/8_01.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJ4-Jb4RRABunNBLw3mg24ekd5A-9nHhob2TWA0c8ofZSfGD4fyqMtVArTORzqRw1BSgaF2fDkp3v5_K1Y20XnnK35oEG1-_T5TsL7gPx3t9kh12pbxEqW481m9yOaq7T500x2-jg5cNE/s1600/8_01.gif" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaQAIBWO3Mq2XKhS6KC1YuHBxe8AY820nnB-s5n6P6DafHeMsHYhL8rUGIaUU4rEY8mQAJwHHBz0bU7xe77bMymPky3ObXIrAtTTNBIwgdf4rLzUk-Q9p4D1k9GvNLjyhcVCPm4vS2yF4/s1600/8_02.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaQAIBWO3Mq2XKhS6KC1YuHBxe8AY820nnB-s5n6P6DafHeMsHYhL8rUGIaUU4rEY8mQAJwHHBz0bU7xe77bMymPky3ObXIrAtTTNBIwgdf4rLzUk-Q9p4D1k9GvNLjyhcVCPm4vS2yF4/s1600/8_02.gif" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwRTNFLGyKAPQLjU_uqjvf8kevS13gZ2Jj1N3ar8HDrP46K8K_9i7bYFn0l9QCiGc1b9HIDQJVblsealasYgS9raZ4wgKlFkdYd9K5kKmZS0IqScjMckecVK8GDY6b1t3AOITIpbPTeTw/s1600/8_03.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwRTNFLGyKAPQLjU_uqjvf8kevS13gZ2Jj1N3ar8HDrP46K8K_9i7bYFn0l9QCiGc1b9HIDQJVblsealasYgS9raZ4wgKlFkdYd9K5kKmZS0IqScjMckecVK8GDY6b1t3AOITIpbPTeTw/s1600/8_03.gif" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi99Dy3NrnFnkO25j5adWJl10yE8ZufKWypQk9gu2o8Ch7LOdqhvshnGT3oD_43uD0O2BiYyIC3oAG6EynHJKz_8QAuW4oVQDcJRoGbMrWv7rGKM-QboLTfI7fmDYuCTvVSAwGqM9ZPLH0/s1600/8_04.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi99Dy3NrnFnkO25j5adWJl10yE8ZufKWypQk9gu2o8Ch7LOdqhvshnGT3oD_43uD0O2BiYyIC3oAG6EynHJKz_8QAuW4oVQDcJRoGbMrWv7rGKM-QboLTfI7fmDYuCTvVSAwGqM9ZPLH0/s1600/8_04.gif" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxL0a_I1GaPwtENLz-g1erkIfyVJQa1yYriujRffBRv4W6UOJda4SmGaYzJ64FiSncW5pPGUGxj17DX1AAuvDAgLFAb7cfskqF90JqjMGLrn_TruBdTD1OC4JzpuROT1UkylqUyKFQHWI/s1600/8_05.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxL0a_I1GaPwtENLz-g1erkIfyVJQa1yYriujRffBRv4W6UOJda4SmGaYzJ64FiSncW5pPGUGxj17DX1AAuvDAgLFAb7cfskqF90JqjMGLrn_TruBdTD1OC4JzpuROT1UkylqUyKFQHWI/s1600/8_05.gif" /></a></div>
<br />
<br />
The code is the next one: <br />
<br />
<blockquote class="tr_bq">
<span style="color: #6aa84f;">from</span> Tkinter<span style="color: #6aa84f;"> import *</span><br />
<br />
root <span style="color: #6aa84f;">=</span> Tk<span style="color: #6aa84f;">()</span><br />
<br />
pic_1 <span style="color: #6aa84f;">=</span> PhotoImage<span style="color: #6aa84f;">(</span><span style="color: #93c47d;">file </span><span style="color: #6aa84f;">=</span> <span style="color: #f1c232;">"8_01.gif"</span><span style="color: #6aa84f;">)</span><br />
pic_2 <span style="color: #6aa84f;">=</span> PhotoImage<span style="color: #6aa84f;">(</span><span style="color: #93c47d;">file </span><span style="color: #6aa84f;">=</span> <span style="color: #f1c232;">"8_02.gif"</span><span style="color: #6aa84f;">)</span><br />
pic_3 <span style="color: #6aa84f;">=</span> PhotoImage<span style="color: #6aa84f;">(</span><span style="color: #93c47d;">file </span><span style="color: #6aa84f;">=</span> <span style="color: #f1c232;">"8_03.gif"</span><span style="color: #6aa84f;">)</span><br />
pic_4 <span style="color: #6aa84f;">=</span> PhotoImage<span style="color: #6aa84f;">(</span><span style="color: #93c47d;">file </span><span style="color: #6aa84f;">=</span> <span style="color: #f1c232;">"8_04.gif"</span><span style="color: #6aa84f;">)</span><br />
pic_5 <span style="color: #6aa84f;">=</span> PhotoImage<span style="color: #6aa84f;">(</span><span style="color: #93c47d;">file </span><span style="color: #6aa84f;">=</span> <span style="color: #f1c232;">"8_05.gif"</span><span style="color: #6aa84f;">)</span><br />
<br />
label <span style="color: #6aa84f;">=</span> Label<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> image <span style="color: #6aa84f;">=</span> pic_5<span style="color: #6aa84f;">)</span><br />
label<span style="color: #6aa84f;">.</span>pack<span style="color: #6aa84f;">()</span><br />
<br />
btn1 <span style="color: #6aa84f;">=</span> Button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> image <span style="color: #6aa84f;">=</span> pic_1<span style="color: #6aa84f;">)</span><br />
btn1<span style="color: #6aa84f;">.</span>pack<span style="color: #6aa84f;">()</span><br />
<br />
btn2 <span style="color: #6aa84f;">=</span> Button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> image <span style="color: #6aa84f;">=</span> pic_2<span style="color: #6aa84f;">)</span><br />
btn2<span style="color: #6aa84f;">.</span>pack<span style="color: #6aa84f;">()</span><br />
<br />
btn3 <span style="color: #6aa84f;">=</span> Button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> image <span style="color: #6aa84f;">=</span> pic_3<span style="color: #6aa84f;">)</span><br />
btn3<span style="color: #6aa84f;">.</span>pack<span style="color: #6aa84f;">()</span><br />
<br />
btn4 <span style="color: #6aa84f;">=</span> Button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> image <span style="color: #6aa84f;">=</span> pic_4<span style="color: #6aa84f;">)</span><br />
btn4<span style="color: #6aa84f;">.</span>pack<span style="color: #6aa84f;">()</span><br />
<br />
root<span style="color: #6aa84f;">.</span>mainloop<span style="color: #6aa84f;">()</span></blockquote>
<br />
<a name='more'></a><br />
Well, you can find more information about the<span style="color: red;"><i><b> PhotoImage </b></i></span>class in the next link: <a href="http://effbot.org/tkinterbook/photoimage.htm">http://effbot.org/tkinterbook/photoimage.htm</a>, the part that I'd like to mention here is that in that article says: "The PhotoImage class can read GIF and PGM/PPM images", which means that if you want to use another type of format, then you should install <a href="http://www.pythonware.com/products/pil/">Python Imaging Library</a> (<b>PIL</b>)<br />
(I just used gif, the other format is unknown for me)<br />
The result is shown in the next picture:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWdWbfNhsQFVjAFfKmXB3bGLLHRFCQtgTY4p867V4VLQrJpry8em5ZoRss5Ux1cOWoF4N-0E0WAT-mOlf79pYXDVENfSSM1i1Go6qIp4jAHUAPmcPnVkbrPTxFV-oYdIIj915b4B4jqzs/s1600/01_pic.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWdWbfNhsQFVjAFfKmXB3bGLLHRFCQtgTY4p867V4VLQrJpry8em5ZoRss5Ux1cOWoF4N-0E0WAT-mOlf79pYXDVENfSSM1i1Go6qIp4jAHUAPmcPnVkbrPTxFV-oYdIIj915b4B4jqzs/s1600/01_pic.JPG" /></a></div>
<br />
As you all can see, this class allows the use of images in <span style="color: orange;"><i><b>Labels</b></i></span> and <span style="color: orange;"><b><i>Buttons</i></b></span>, of course that this is just the begining, there are ways in which one could manage the the organization and position of everything.Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com1tag:blogger.com,1999:blog-3250011985483393306.post-83838092378209341432015-04-14T20:29:00.001-07:002015-04-21T20:22:55.246-07:00Using Tkinter with a class methodIn this post I did a remake of another previous example,<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9Z0OiehI5n_RIFFZjrpNn7ETlB8hg_XpkWKAhTsb5yd03RpG_pgP7R28kNznddsxMdnUJbtHLcnsz04WiBLTY5pRXbG1TvP6bi_BeLVetHjT5incFjO8nqSgioXBfVPpXs3DZdrCBN7A/s1600/01_p1c7.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9Z0OiehI5n_RIFFZjrpNn7ETlB8hg_XpkWKAhTsb5yd03RpG_pgP7R28kNznddsxMdnUJbtHLcnsz04WiBLTY5pRXbG1TvP6bi_BeLVetHjT5incFjO8nqSgioXBfVPpXs3DZdrCBN7A/s1600/01_p1c7.jpg" height="152" width="320" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
but in this case I'm using a class method mixed with the previous way of using the "root = Tk()", well in this case this was just a simple way to change the title, which can be seen in the final parts of the code.<br />
<br />
<blockquote class="tr_bq">
<span style="color: #6aa84f;">from</span> Tkinter <span style="color: #6aa84f;">import *</span><br />
<br />
<span style="color: #6aa84f;">class</span><b> <span style="color: #e06666;">HH_set</span></b><span style="color: #6aa84f;">:</span><br />
<br />
<span style="color: #6aa84f;">def</span> <span style="color: #e06666;">__init__</span><span style="color: #6aa84f;">(</span>self, master<span style="color: #6aa84f;">):</span><br />
frame <span style="color: #6aa84f;">= </span>Frame<span style="color: #6aa84f;">(</span>master<span style="color: #6aa84f;">)</span><br />
frame<span style="color: #6aa84f;">.</span>pack<span style="color: #6aa84f;">()</span><br />
<br />
self<span style="color: #6aa84f;">.</span>songLabel <span style="color: #6aa84f;">= </span>Label<span style="color: #6aa84f;">(</span>frame<span style="color: #6aa84f;">,</span> text =<span style="color: #bf9000;"> "Spoon - 'Got Nuffin'. Available this summer!"</span><span style="color: #6aa84f;">)</span><br />
self<span style="color: #6aa84f;">.</span>songLabel.pack<span style="color: #6aa84f;">()</span><br />
<br />
self.xclmLabel <span style="color: #6aa84f;">= </span>Label(frame, text <span style="color: #6aa84f;">= </span>"Yeah!")<br />
self.xclmLabel.pack<span style="color: #6aa84f;">()</span><br />
<br />
self.hearButton <span style="color: #6aa84f;">= </span>Button(frame, text <span style="color: #6aa84f;">= </span><span style="color: #bf9000;">"Let's hear it"</span>, state <span style="color: #6aa84f;">= </span>DISABLED)<br />
self.hearButton.pack(side <span style="color: #6aa84f;">= </span>LEFT)<br />
<br />
self.seeButton <span style="color: #6aa84f;">= </span>Button(frame, text <span style="color: #6aa84f;">=</span><span style="color: #bf9000;"><span style="color: #6aa84f;"> </span>"Let's see"</span>, state <span style="color: #6aa84f;">= </span>DISABLED)<br />
self.seeButton.pack(side <span style="color: #6aa84f;">= </span>RIGHT)<br />
<br />
self.tuneButton <span style="color: #6aa84f;">= </span>Button(frame, text =<span style="color: #bf9000;"> "Tune"</span>, command <span style="color: #6aa84f;">= </span>self.printMessage)<br />
self.tuneButton.pack(side <span style="color: #6aa84f;">= </span>RIGHT)<br />
<br />
<span style="color: #6aa84f;">def</span> <span style="color: #e06666;">printMessage</span>(self):<br />
print <span style="color: #bf9000;">"Channel open"</span><br />
<br />
root <span style="color: #6aa84f;">= </span>Tk<span style="color: #6aa84f;">()</span><br />
root<span style="color: #6aa84f;">.</span>title<span style="color: #6aa84f;">(</span><span style="color: #bf9000;">"Happy Hour"</span><span style="color: #6aa84f;">)</span><br />
<br />
base_object <span style="color: #6aa84f;">= </span>HH_set<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">)</span><br />
<br />
root<span style="color: #6aa84f;">.</span>mainloop<span style="color: #6aa84f;">()</span></blockquote>
<br />
<a name='more'></a>At the begining of the class HH_set, it's defined the function "<span style="color: #e06666;">__init__</span>" with the arguments "self" and "master". After that a frame its created and packed.<br />
<br />
<blockquote class="tr_bq">
frame <span style="color: #6aa84f;">= </span>Frame(master)<br />
frame<span style="color: #6aa84f;">.</span>pack<span style="color: #6aa84f;">()</span></blockquote>
<br />
The labels and buttons are created with a "self." before the name assigned, like:<br />
<br />
<blockquote class="tr_bq">
self<span style="color: #6aa84f;">.</span>songLabel <span style="color: #6aa84f;">= </span>Label<span style="color: #6aa84f;">(</span>frame<span style="color: #6aa84f;">, </span>text <span style="color: #6aa84f;">=</span><span style="color: #bf9000;"> "Spoon - 'Got Nuffin'. Available this summer!"</span><span style="color: #6aa84f;">)</span><br />
self<span style="color: #6aa84f;">.</span>songLabel<span style="color: #6aa84f;">.</span>pack<span style="color: #6aa84f;">()</span></blockquote>
<br />
That's the only variant, which in the first posts is not used because they were pretty straightforward and easy to grasp.<br />
<br />
Almost in the end, there is the use of the class "HH_set" (which could've been any other name) in order to create all that was in 'contents'.<br />
<br />
<blockquote class="tr_bq">
base_object<span style="color: #6aa84f;"> = </span>HH_set<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;"><span style="color: black;"></span>)</span></blockquote>
Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com0tag:blogger.com,1999:blog-3250011985483393306.post-43850648701918770902015-04-13T18:39:00.001-07:002015-04-15T13:03:37.393-07:00Using a class to create a buttonThis time I'm using something that is new for me (at least when I'm writing this post :P), which is the use of a class (along with functions) in order to create a button with TKinter. The result is similar to one of the first examples that I've posted, but the code is more complex.<br />
<br />
<br />
<blockquote class="tr_bq">
<span style="color: #6aa84f;">from</span> Tkinter <span style="color: #6aa84f;">import *</span><br />
<span style="color: #6aa84f;">def</span> <span style="color: #e06666;">frame</span><span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">, </span>side<span style="color: #6aa84f;">):</span><br />
<span style="color: #999999;"># wf : Window Frame</span><br />
wf <span style="color: #6aa84f;">= </span>Frame(root)<br />
wf<span style="color: #6aa84f;">.</span>pack<span style="color: #6aa84f;">(</span>side<span style="color: #6aa84f;">=</span>side, expand<span style="color: #6aa84f;">=</span>YES, fill<span style="color: #6aa84f;">=</span>BOTH<span style="color: #6aa84f;">)</span><br />
<span style="color: #6aa84f;">return </span>wf<br />
<br />
<span style="color: #6aa84f;">def</span> <span style="color: #e06666;">button</span><span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> side<span style="color: #6aa84f;">,</span> text<span style="color: #6aa84f;">, </span>command<span style="color: #6aa84f;">=</span><span style="color: #b6d7a8;">None</span><span style="color: #6aa84f;">):</span><br />
btn <span style="color: #6aa84f;">= </span>Button<span style="color: #6aa84f;">(</span>root, text<span style="color: #6aa84f;">=</span>text, command<span style="color: #6aa84f;">=</span>command<span style="color: #6aa84f;">)</span><br />
btn<span style="color: #6aa84f;">.</span>pack<span style="color: #6aa84f;">(</span>side<span style="color: #6aa84f;">=</span>side, expand<span style="color: #6aa84f;">=</span>YES, fill<span style="color: #6aa84f;">=</span>BOTH<span style="color: #6aa84f;">)</span><br />
<span style="color: #6aa84f;">return </span>btn<br />
<br />
<span style="color: #6aa84f;">class </span><span style="color: #e06666;">Sample</span><span style="color: #6aa84f;">(</span>Frame<span style="color: #6aa84f;">):</span><br />
<span style="color: #6aa84f;">def </span><span style="color: #e06666;">__init__</span><span style="color: #6aa84f;">(</span>self<span style="color: #6aa84f;">):</span><br />
Frame.<span style="color: lime;">__init__</span>(self)<br />
self.pack<span style="color: #6aa84f;">(</span>expand<span style="color: #6aa84f;">=</span>YES, fill<span style="color: #6aa84f;">=</span>BOTH<span style="color: #6aa84f;">)</span><br />
self.master.title<span style="color: #6aa84f;">(</span><span style="color: orange;">"Simpl3"</span><span style="color: #6aa84f;">)</span><br />
self.master.iconname<span style="color: #6aa84f;">(</span><span style="color: orange;">"smpl3"</span><span style="color: #6aa84f;">)</span><br />
<br />
opsF <span style="color: #6aa84f;">= </span>frame<span style="color: #6aa84f;">(</span>self<span style="color: #6aa84f;">,</span> TOP<span style="color: #6aa84f;">)</span><br />
btn <span style="color: #6aa84f;">= </span>button<span style="color: #6aa84f;">(</span>opsF<span style="color: #6aa84f;">,</span> LEFT<span style="color: #6aa84f;">,</span> <span style="color: orange;">"About time!"</span><span style="color: #6aa84f;">)</span><br />
<br />
<span style="color: #6aa84f;">if </span><span style="color: lime;">__name__</span> <span style="color: #6aa84f;">== </span><span style="color: orange;">"__main__"</span><span style="color: #6aa84f;">:</span><br />
Sample<span style="color: #6aa84f;">().</span>mainloop<span style="color: #6aa84f;">()</span></blockquote>
<br />
<a name='more'></a><br />
<br />
<br />
I'm not familiar with this way of Python programming, so I used as reference a sample that uses even more wierd stuff that I still haven't figured out, so I've decided to post what I could understand. Anyway, the result is a clickable button:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrnZRjXZqFNb-UEI9AcO6ZAXDEqUn8So0LRxo-c-JWJ6E2wIGq7J2CJNzNPxbmOBwnJxz7kdX4dbYw1x2uQP31k7GCADpUaPmUD3dG5zHym1ggpJ3WACc9BYV2z9FIhfxssPnnI9E1_So/s1600/01_c1p6.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrnZRjXZqFNb-UEI9AcO6ZAXDEqUn8So0LRxo-c-JWJ6E2wIGq7J2CJNzNPxbmOBwnJxz7kdX4dbYw1x2uQP31k7GCADpUaPmUD3dG5zHym1ggpJ3WACc9BYV2z9FIhfxssPnnI9E1_So/s1600/01_c1p6.JPG" /></a></div>
<br />
<br />
<br />
The last two lines are supposed to be present in every code that uses the <b>class</b> mechanism, except that for this case we're going to use a combination with the<b> mainloop</b> '<i>method</i>'.<br />
<br />
<br />
<br />
The functions 'frame' and 'button' have been created so we could simplify and organize our code a little better.<br />
<br />
<br />
The function '<span style="color: red;">__init__</span>' inside the '<span style="color: #e06666;">Sample</span>' class use the <i><b>master</b></i> method, which takes care of the title and the iconname. After that, we create a frame and a button with their respective names.Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com0tag:blogger.com,1999:blog-3250011985483393306.post-16886186687696565942015-04-13T14:08:00.002-07:002018-01-29T19:33:55.300-08:00Creating a simple square root calculatorWith the next code you will be able to use the input from the user an display the result in the created window by clicking a button with an assignated command.<br />
<br />
<blockquote class="tr_bq">
<span style="color: #999999;"># Calculating x**0.5</span><br />
<span style="color: #6aa84f;">from </span>Tkinter <span style="color: #6aa84f;">import *</span><br />
<br />
<span style="color: #6aa84f;">def <span style="color: #e06666;">square_calc</span>():</span><br />
x <span style="color: #6aa84f;">=</span> x_val.get<span style="color: #6aa84f;">()</span><br />
sqx <span style="color: #6aa84f;">=</span> x <span style="color: #6aa84f;">**</span> 0.5<br />
print <span style="color: #f1c232;">"x ="</span>, x, <span style="color: #f1c232;">"\t"</span>, x, <span style="color: #f1c232;">"** 0.5 ="</span>, sqx<br />
sqx_txt <span style="color: #6aa84f;">=</span> Label(root, text <span style="color: #6aa84f;">=</span> <span style="color: #f1c232;">"x ** 0.5 ="</span>).grid(row<span style="color: #6aa84f;">=</span>3, column<span style="color: #6aa84f;">=</span>0)<br />
sqx_lab <span style="color: #6aa84f;">= </span>Label(root, text <span style="color: #6aa84f;">=</span> sqx).grid(row<span style="color: #6aa84f;">=</span>3, column<span style="color: #6aa84f;">=</span>1)<br />
<br />
root <span style="color: #6aa84f;">= </span>Tk<span style="color: #6aa84f;">()</span><br />
root<span style="color: #6aa84f;">.</span>title<span style="color: #6aa84f;">(<span style="color: #f1c232;">"Calculating square root"</span>)</span><br />
<br />
x_val <span style="color: #6aa84f;">= </span>DoubleVar<span style="color: #6aa84f;">()</span><br />
<br />
x_lab <span style="color: #6aa84f;"> =</span> Label(root, text <span style="color: #6aa84f;">=</span><span style="color: #f1c232;"> "x"</span>).grid(row<span style="color: #6aa84f;">=</span>0, column<span style="color: #6aa84f;">=</span>0)<br />
nmb <span style="color: #6aa84f;"> =</span> Entry(root, textvariable = x_val).grid(row<span style="color: #6aa84f;">=</span>0, column<span style="color: #6aa84f;">=</span>1)<br />
calc <span style="color: #6aa84f;"> =</span> Button(root, text <span style="color: #6aa84f;">=</span> <span style="color: #f1c232;">"Calculate"</span><span style="color: #6aa84f;">,</span> command<span style="color: #6aa84f;">=</span>square_calc).grid(columnspan<span style="color: #6aa84f;">=</span>2)<br />
y_lab <span style="color: #6aa84f;">=</span> Label(root, text <span style="color: #6aa84f;">=</span> <span style="color: #f1c232;">" "</span>).grid(row<span style="color: #6aa84f;">=</span>3, column<span style="color: #6aa84f;">=</span>0)<br />
<br />
root.mainloop<span style="color: #6aa84f;">()</span></blockquote>
<br />
<br />
The result would be like this:<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKcc5au_QYC8t6zZb4g0k1REM6hJVg0qAc9eqP4vLW1P22n8Jjl5Acqo5MwO-N6xPN27_Ry8b4IBR-fOPwFE0ORoxPK41WqvKeWHYy3sk7W3s0cu-Ai_vh4jrCrry3WVfwau4GfzTlFNg/s1600/01_c1p5.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKcc5au_QYC8t6zZb4g0k1REM6hJVg0qAc9eqP4vLW1P22n8Jjl5Acqo5MwO-N6xPN27_Ry8b4IBR-fOPwFE0ORoxPK41WqvKeWHYy3sk7W3s0cu-Ai_vh4jrCrry3WVfwau4GfzTlFNg/s1600/01_c1p5.jpg" /></a></div>
<br />
<a name='more'></a><br />
<br />
<br />
Now you can try with a number<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizFztImrbNvgTWhTO_yxjwkwZH8UMpsQJtUwn-pFK-p-GkP0_2rld4k6hPsxAQD-Okb0sMuXqozNexi9kObMkj2ujzmQMog9gfD3FLnNdP0PxOiSXv__WaOFJUfIxREbA9ChLkeChk-4U/s1600/02_c1p5.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizFztImrbNvgTWhTO_yxjwkwZH8UMpsQJtUwn-pFK-p-GkP0_2rld4k6hPsxAQD-Okb0sMuXqozNexi9kObMkj2ujzmQMog9gfD3FLnNdP0PxOiSXv__WaOFJUfIxREbA9ChLkeChk-4U/s1600/02_c1p5.jpg" /></a></div>
<br />
<br />
But there is a problem, if you put a number with an exact square root like '4', then the numeric result will get all mixed up with the previous answer.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzKM78Hq3ev9s5lGZvPfejtXEv0K1X-x5g_Y3IoeoEHmZbYPSDE7Z6M6HIiZFdy7juLCN_piaLtimryNZ5gtM9oP_dJ7kkEyRsO9-yEv5tD-KmfTLjDyX4zmNKNXlRTZAapauPI29WTUs/s1600/03_c1p5.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzKM78Hq3ev9s5lGZvPfejtXEv0K1X-x5g_Y3IoeoEHmZbYPSDE7Z6M6HIiZFdy7juLCN_piaLtimryNZ5gtM9oP_dJ7kkEyRsO9-yEv5tD-KmfTLjDyX4zmNKNXlRTZAapauPI29WTUs/s1600/03_c1p5.jpg" /></a></div>
<br />
<br />
The solution that I found out after searching here and there, even asking in Stack Overflow, is the next code: <br />
<blockquote class="tr_bq">
from Tkinter import *<br />
<br />
<span style="color: #6aa84f;">def <span style="color: #e06666;">square_calc</span>():</span><br />
x <span style="color: #6aa84f;">=</span> x_val.get()<br />
sqx <span style="color: #6aa84f;">=</span> x <span style="color: #6aa84f;">**</span> 0.5<br />
sqx_txt <span style="color: #6aa84f;">=</span> Label<span style="color: #6aa84f;">(</span>root, text = <span style="color: #f1c232;">"x ** 0.5 ="<span style="color: #6aa84f;">)</span>.</span>grid<span style="color: #6aa84f;">(</span>row<span style="color: #6aa84f;">=</span>3, column<span style="color: #6aa84f;">=</span>0<span style="color: #6aa84f;">)</span><br />
sqx_lab.config(text<span style="color: #6aa84f;">=</span>sqx)<br />
sqx_lab.grid(row<span style="color: #6aa84f;">=</span>3, column<span style="color: #6aa84f;">=</span>1, sticky<span style="color: #6aa84f;">=</span>W)<br />
<br />
root <span style="color: #6aa84f;">= </span>Tk<span style="color: #6aa84f;">()</span><br />
root.title<span style="color: #6aa84f;">(<span style="color: #f1c232;">"Calculating square root"</span>)</span><br />
<br />
x_val <span style="color: #6aa84f;">=</span> DoubleVar<span style="color: #6aa84f;">()</span><br />
<br />
x_lab <span style="color: #6aa84f;">=</span> Label(root, text <span style="color: #6aa84f;">= </span><span style="color: #f1c232;">"x"</span>).grid(row<span style="color: #6aa84f;">=</span>0, column<span style="color: #6aa84f;">=</span>0)<br />
nmb <span style="color: #6aa84f;">=</span> Entry(root, textvariable <span style="color: #6aa84f;">=</span> x_val).grid(row<span style="color: #6aa84f;">=</span>0, column<span style="color: #6aa84f;">=</span>1)<br />
calc <span style="color: #6aa84f;">=</span> Button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">, </span>text =<span style="color: #f1c232;"> "Calculate"</span>, command<span style="color: #6aa84f;">=</span>square_calc<span style="color: #6aa84f;">).</span>grid(columnspan<span style="color: #6aa84f;">=</span>2)<br />
y_lab <span style="color: #6aa84f;">=</span> Label(root, text <span style="color: #6aa84f;">=</span> <span style="color: #f1c232;">" "</span>).grid(row<span style="color: #6aa84f;">=</span>3, column<span style="color: #6aa84f;">=</span>0)<br />
sqx_lab <span style="color: #6aa84f;">= </span>Label(root, text <span style="color: #6aa84f;">=</span> <span style="color: #f1c232;">" "</span>)<br />
<br />
root<span style="color: #6aa84f;">.</span>mainloop<span style="color: #6aa84f;">()</span></blockquote>
<br />
Which gets rid of that little problem.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhU8yMIxu0q8QUGhLRKkOhfOaLw2hbeU8MRrhGbOKLhobmyVSj4-89zcGW9URju8sK4c18FnCHWVsvyPta8ASm-cZijcf2K8sIRTJpj9pD1H3pNQ2jqQfkoYc3Nzl_vZMmA6xBcqQeCz9o/s1600/04_c1p5.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhU8yMIxu0q8QUGhLRKkOhfOaLw2hbeU8MRrhGbOKLhobmyVSj4-89zcGW9URju8sK4c18FnCHWVsvyPta8ASm-cZijcf2K8sIRTJpj9pD1H3pNQ2jqQfkoYc3Nzl_vZMmA6xBcqQeCz9o/s1600/04_c1p5.jpg" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh91FuTxQqzRPDU8oELQi_3_Z-QZmzje5q0ialjY2KQKBWQVQPmNyknxEYNokXI0syl9zdR9EY1Qu2vZs_7TPhINrstlzGjfH4NZ9ZwqjKmOdVPGKjfRMhiqdfshmKv_hIeU-au3-AkDP0/s1600/05_c1p5.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh91FuTxQqzRPDU8oELQi_3_Z-QZmzje5q0ialjY2KQKBWQVQPmNyknxEYNokXI0syl9zdR9EY1Qu2vZs_7TPhINrstlzGjfH4NZ9ZwqjKmOdVPGKjfRMhiqdfshmKv_hIeU-au3-AkDP0/s1600/05_c1p5.jpg" /></a></div>
<br />
<br />
It seems necessary that I should mention the fact that I've been adviced about getting the habit of using classes, which means to use object oriented programmation in order to keep any wierd problem at bay, I suppose that's why got that 'bug' with my initial code.Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com0tag:blogger.com,1999:blog-3250011985483393306.post-54920959745244064032015-04-13T13:32:00.000-07:002015-04-15T12:46:05.187-07:00Using the 'command' option of the Button widgetIn this post I will show how to put some buttons and assign a command to one of them, yeah, at last something with functionality :P <br />
<br />
Well, first goes the code like this:<br />
<blockquote class="tr_bq">
<span style="color: #6aa84f;">from </span>Tkinter <span style="color: #6aa84f;">import *</span><br />
<br />
<span style="color: #6aa84f;">def </span><span style="color: #e06666;">printMessage</span><span style="color: #6aa84f;">():</span><br />
print <span style="color: #f1c232;">"Channel open"</span><br />
<br />
root <span style="color: #6aa84f;">= </span>Tk<span style="color: #6aa84f;">()</span><br />
root<span style="color: #6aa84f;">.</span>title<span style="color: #6aa84f;">(<span style="color: #f1c232;">"Happy hour"</span>)</span><br />
label_1 <span style="color: #6aa84f;">= </span>Label<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">, </span>text <span style="color: #6aa84f;">=</span><span style="color: #f1c232;"><span style="color: #6aa84f;"> </span>"Spoon - 'Got Nuffin'. Available since this summer!"</span><span style="color: #6aa84f;">)</span><br />
label_2 <span style="color: #6aa84f;">= </span>Label<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">, </span>text <span style="color: #6aa84f;">= </span><span style="color: #f1c232;">"Yeah!"</span><span style="color: #6aa84f;">)</span><br />
label_1<span style="color: #6aa84f;">.</span>pack<span style="color: #6aa84f;">()</span><br />
label_2<span style="color: #6aa84f;">.</span>pack<span style="color: #6aa84f;">()</span><br />
Button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> text <span style="color: #6aa84f;">= </span><span style="color: #f1c232;">"Let's hear it"</span><span style="color: #6aa84f;">,</span> state <span style="color: #6aa84f;">= </span>DISABLED<span style="color: #6aa84f;">).</span>pack<span style="color: #6aa84f;">(</span>side <span style="color: #6aa84f;">= </span>LEFT<span style="color: #6aa84f;">)</span><br />
Button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> text <span style="color: #6aa84f;">= </span><span style="color: #f1c232;">"Let's see"</span><span style="color: #6aa84f;">).</span>pack<span style="color: #6aa84f;">(</span>side <span style="color: #6aa84f;">= </span>RIGHT<span style="color: #6aa84f;">)</span><br />
Button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> text <span style="color: #6aa84f;">=</span> <span style="color: #f1c232;">"Tune"</span><span style="color: #6aa84f;">,</span> command <span style="color: #6aa84f;">= </span>printMessage<span style="color: #6aa84f;">).</span>pack<span style="color: #6aa84f;">(</span>side <span style="color: #6aa84f;">= </span>RIGHT<span style="color: #6aa84f;">)</span><br />
root.mainloop<span style="color: #6aa84f;">()</span></blockquote>
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<br />
<br />
When running the previous code, you should get something like the next picture:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhavdtWZFuWzlbY4mV-TEs22KAxazRkTHv129h3JGTMbdcc0htkpToDVNn09-2V42rB_4OdLqnDVNoaVeCvKLURt3z4S09HDLR8ZKl17PWvYlAAWIB6Qe4sqaDdqESYR1kVfvfNIjxJXKQ/s1600/01_c1p4.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhavdtWZFuWzlbY4mV-TEs22KAxazRkTHv129h3JGTMbdcc0htkpToDVNn09-2V42rB_4OdLqnDVNoaVeCvKLURt3z4S09HDLR8ZKl17PWvYlAAWIB6Qe4sqaDdqESYR1kVfvfNIjxJXKQ/s1600/01_c1p4.jpg" /></a></div>
<br />
<br />
<a name='more'></a><br />
<br />
If you haven't noticed, the button that says "Let's hear it" its gray and cannot be clicked like the other buttons in the created window. If you look the part of the code in which the respective button has the text "Let's hear it", you can see that it has been used the option "state" of the Button widget, in this case it is selected the option "DISABLED".<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0MZNkoBW7T2rKRjUMLJ_8MZsis3NMiV3cdidQ_g0yWlEjbO5tkWmdbGOJh9_SkI-InCAc7FPwaIQ1JPimzCVdy6rHWjBHd5fl-zBHjnyaOULUnEzIMzS-_5S2QDB9bwqsG-rcwEllEb0/s1600/02_c1p4.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0MZNkoBW7T2rKRjUMLJ_8MZsis3NMiV3cdidQ_g0yWlEjbO5tkWmdbGOJh9_SkI-InCAc7FPwaIQ1JPimzCVdy6rHWjBHd5fl-zBHjnyaOULUnEzIMzS-_5S2QDB9bwqsG-rcwEllEb0/s1600/02_c1p4.jpg" /></a></div>
<br />
The phrase "Available since this summer!" does't sound well to me, but I decided to leave it like that, at least for some time.<br />
<br />
Anyway, back to the code, there is a function called printMessage, well, at the moment that it's assigned a command to a button, one must use the "command" option of the Button widget, and just put then name of the function without the parenthesis.<br />
<br />
Of course the result of clicking the button will be related to the command assigned, in this case is just a text printed in the console window.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyyajZjwvOK2yYewFOcuObFDhnc5G2qBL1EFnxPP15f-2JVhKCtafmGofHSlaPOTrsvTSkjR5nFi54N08uPljdYVwoFPiFcjhwvIyiCptSSs6B7gq1nH8CwqvnT56dDnw4Fe76zhOygP8/s1600/03_c1p4.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyyajZjwvOK2yYewFOcuObFDhnc5G2qBL1EFnxPP15f-2JVhKCtafmGofHSlaPOTrsvTSkjR5nFi54N08uPljdYVwoFPiFcjhwvIyiCptSSs6B7gq1nH8CwqvnT56dDnw4Fe76zhOygP8/s1600/03_c1p4.jpg" height="178" width="320" /></a></div>
<br />
<br />
And the last thing that I'm going to mention here is that in this code we opted to use the "pack" method, and also we used its options, like using the allignemts with the "side" method. The first button assigned to the left, the other two for the right side, but the order of the buttons is like: the first one packed is put to the right side, then the next one is put at the left side of the first one, and so this will be the way in which the other created buttons will be organizated. <br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIIapweSeoV_sa5HqE04dZZjlVwne0LNvLuQOLDobIYGXZZ8DdSYnZd3de4BFufPxI6C26ED_5otaUAM4TqwhivD_rJqZwZbTgUDkB1YNUIMcE1wh_GELmTb9RTVXbcllYUcAkPwGNU_o/s1600/04_c1p4.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIIapweSeoV_sa5HqE04dZZjlVwne0LNvLuQOLDobIYGXZZ8DdSYnZd3de4BFufPxI6C26ED_5otaUAM4TqwhivD_rJqZwZbTgUDkB1YNUIMcE1wh_GELmTb9RTVXbcllYUcAkPwGNU_o/s1600/04_c1p4.jpg" /></a></div>
Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com0tag:blogger.com,1999:blog-3250011985483393306.post-4905557278902578352015-03-25T20:23:00.000-07:002015-04-15T12:33:25.972-07:00Creating ButtonIn this code samples it is shown how to create buttons in a simple way, albeit it could get complicated at the end.<br />
<br />
The first go like this:<br />
<br />
<blockquote class="tr_bq">
<span style="color: #6aa84f;">import </span>Tkinter<br />
root <span style="color: #6aa84f;">= </span>Tkinter<span style="color: #6aa84f;">.</span>Tk<span style="color: #6aa84f;">()</span><br />
button <span style="color: #6aa84f;">= </span>Tkinter.Button<span style="color: #6aa84f;">(</span>root, text <span style="color: #6aa84f;">= </span><span style="color: #f1c232;">"Forgotten Sign"</span><span style="color: #6aa84f;">).</span>pack<span style="color: #6aa84f;">()</span><br />
root<span style="color: #6aa84f;">.</span>mainloop<span style="color: #6aa84f;">()</span> </blockquote>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUzH0dZxCOqgtkAN0VyFD-Fmtapw32Jw-pDFe4oihwLyYXT1VMpgXFptyDq6oD_-242OMEybA3xUmp9WGb05zy99JKoEJoQknANoA7I0G55wbjUjqDZ5VfEs6TqmdJMSPQD1ZUjQWjg9I/s1600/01_01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><br /></a></div>
<br />
Which ends up like this:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEVXglqZuJlmsuaVwTSkt9PAaGiEKCC_p576LXnVpGTVJPi6AjHQo-hzAotoC1tqIXK4fldCDs_JVxOqi19G07Y4OjoXvrql8-8ry4GBvI2ITfqNh80RaIhzNbocIPtTZEXCyb5lV6KrE/s1600/01_02.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEVXglqZuJlmsuaVwTSkt9PAaGiEKCC_p576LXnVpGTVJPi6AjHQo-hzAotoC1tqIXK4fldCDs_JVxOqi19G07Y4OjoXvrql8-8ry4GBvI2ITfqNh80RaIhzNbocIPtTZEXCyb5lV6KrE/s1600/01_02.png" height="148" width="320" /></a></div>
<br />
<br />
<a name='more'></a><br />
The other one is:<br />
<br />
<blockquote class="tr_bq">
<span style="color: #6aa84f;">import </span>Tkinter<br />
root <span style="color: #6aa84f;">= </span>Tkinter.Tk()<br />
button1 <span style="color: #6aa84f;">= </span>Tkinter.Button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">, </span>text <span style="color: #6aa84f;">=</span><span style="color: #f1c232;"><span style="color: #6aa84f;"> </span>"Forgotten Sign"</span><span style="color: #6aa84f;">).</span>pack<span style="color: #6aa84f;">()</span><br />
button2 <span style="color: #6aa84f;">= </span>Tkinter.Button<span style="color: #6aa84f;">(</span>root<span style="color: #6aa84f;">,</span> text <span style="color: #6aa84f;">= </span><span style="color: #f1c232;">"Warning"</span><span style="color: #6aa84f;">).</span>pack<span style="color: #6aa84f;">()</span><br />
root<span style="color: #6aa84f;">.</span>mainloop<span style="color: #6aa84f;">()</span></blockquote>
<br />
And the result is:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvUWJaHY2ErGzrlFoZ3Wl48dN_HzsL9SM0AO8OXO2idlEhMbGSr7gqqSSn4iIqwsKGGvlQbWV0AowdlDDB1VTHPw_w1526Yesi8-tlC4SSQm526Zfuvdorla4rPpTsh3l-wI3Rs9q9ZXE/s1600/02_01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvUWJaHY2ErGzrlFoZ3Wl48dN_HzsL9SM0AO8OXO2idlEhMbGSr7gqqSSn4iIqwsKGGvlQbWV0AowdlDDB1VTHPw_w1526Yesi8-tlC4SSQm526Zfuvdorla4rPpTsh3l-wI3Rs9q9ZXE/s1600/02_01.png" height="173" width="320" /></a></div>
The last one is making some order in it:<br />
<blockquote class="tr_bq">
<br />
<span style="color: #6aa84f;">from </span>Tkinter <span style="color: #6aa84f;">import *</span><br />
root <span style="color: #6aa84f;">= </span>Tk<span style="color: #6aa84f;">()</span><br />
button1 <span style="color: #6aa84f;">= </span>Button(root<span style="color: #6aa84f;">,</span> text <span style="color: #6aa84f;">= </span><span style="color: #f1c232;">"Forgotten Sign"</span>).pack(side <span style="color: #6aa84f;">= </span>LEFT)<br />
button2 <span style="color: #6aa84f;">= </span>Button(root<span style="color: #6aa84f;">, </span>text <span style="color: #6aa84f;">=<span style="color: #f1c232;"> </span></span><span style="color: #f1c232;">"Warning"</span>).pack(side <span style="color: #6aa84f;">= </span>LEFT)<br />
root<span style="color: #6aa84f;">.</span>mainloop<span style="color: #6aa84f;">()</span></blockquote>
<br />
Which gets like this:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihmMiKWcrOp7rVQtjhu9Kq4rIQ8rbAE9Pgejx7s2DgUUebD2X9tXM7ZT1BCPdDIIdUkAieJ8D4hC0sWPdhCgqe0Hlr1KEvbEpydst_-4Sf59GcDm_ewkNvRZ0axwZSROjG4msrvXaEpVo/s1600/03_01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihmMiKWcrOp7rVQtjhu9Kq4rIQ8rbAE9Pgejx7s2DgUUebD2X9tXM7ZT1BCPdDIIdUkAieJ8D4hC0sWPdhCgqe0Hlr1KEvbEpydst_-4Sf59GcDm_ewkNvRZ0axwZSROjG4msrvXaEpVo/s1600/03_01.png" height="139" width="320" /></a></div>
Of course these buttons just are clickable, this is because we haven't put any other stuff like commands or any other decoration, which is why the code is so compact and without changing the title of the window program :PJenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com0tag:blogger.com,1999:blog-3250011985483393306.post-37797455335452044152015-03-23T18:45:00.002-07:002015-04-15T12:17:49.005-07:00Changing The Text Title<div align="justify">
In the post "Starting Codes" I mentioned about certain things about some inconvenients when using a total import (the so called 'star import'), paradoxically, I haven't found any code that goes without this 'star import' because all of them are using the </div>
<blockquote class="tr_bq">
root <span style="color: #6aa84f;">= </span>Tk<span style="color: #6aa84f;">() </span></blockquote>
<blockquote class="tr_bq">
root<span style="color: #6aa84f;">.</span>title<span style="color: #6aa84f;">(</span><span style="color: #bf9000;">"Title text that can be changed"</span><span style="color: #6aa84f;">)</span>
</blockquote>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhd1svJp5k1wpZ0mt5_x_fBIPYX_EYwfmcHE6d3Fhh2Ejw9QUPd-Uq5JIO6GvCU2_GHn7ClcdTV3HcvOLypu3nmFIgus5G1dg2-bYiLDH46ATOCKBWeiDK2FWfQ3A3oPTTgXu5Dgk-65xU/s1600/02_code_01_01.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhd1svJp5k1wpZ0mt5_x_fBIPYX_EYwfmcHE6d3Fhh2Ejw9QUPd-Uq5JIO6GvCU2_GHn7ClcdTV3HcvOLypu3nmFIgus5G1dg2-bYiLDH46ATOCKBWeiDK2FWfQ3A3oPTTgXu5Dgk-65xU/s1600/02_code_01_01.jpeg" height="139" width="320" /></a></div>
<br />
<a name='more'></a><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrTy3FCt9GStOPAZrBIWS4BeqGX_3Ccaayrk5V3FEOBCowcqBQ5HPa4i-zTvxEje6_W5qXK8MjTfRLj4S8E7HNd1gcZmSrAxkT-tIyQu-5kCXANsvgnKGKubQ-Cm4J01HXTUVz-5ja_ZA/s1600/02_code_01_02.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrTy3FCt9GStOPAZrBIWS4BeqGX_3Ccaayrk5V3FEOBCowcqBQ5HPa4i-zTvxEje6_W5qXK8MjTfRLj4S8E7HNd1gcZmSrAxkT-tIyQu-5kCXANsvgnKGKubQ-Cm4J01HXTUVz-5ja_ZA/s1600/02_code_01_02.jpeg" height="76" width="320" /></a></div>
<br />
<br />
<br />
<div align="justify">
Anyway, let's see put those lines and check the result, which should be like this:</div>
<div align="justify">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg51xDXy6mSKVZLBCXfOvg01pCE3M-Ks5Dvi4Waf6_IfyPw35BS_JqvBebvLTzoz8Lw-7VVVqw3yCfe_RB4u-y7cq6MXbuBsYB5obyPnngBkf8vA_2w7yCsnqHW4DMHOvXz83SVDtgGUhs/s1600/02_code_01_03.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg51xDXy6mSKVZLBCXfOvg01pCE3M-Ks5Dvi4Waf6_IfyPw35BS_JqvBebvLTzoz8Lw-7VVVqw3yCfe_RB4u-y7cq6MXbuBsYB5obyPnngBkf8vA_2w7yCsnqHW4DMHOvXz83SVDtgGUhs/s1600/02_code_01_03.jpeg" /></a></div>
<div align="justify">
<br /></div>
Or like this :
All depends on what text you type in.<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5D4tEgSZtiJ8nkGfmfHMPG8u0R4nUbHbBU0xMWK1_s08pxsu382PoQ574Oy7RNvWYycUmbadbUlL_r0IFxMFWgiQJAyUyYR1O5gCPKlsawMeUjHlhxx5NWUTLn6zkNbk-PXDT8JYETMs/s1600/02_code_01_04.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5D4tEgSZtiJ8nkGfmfHMPG8u0R4nUbHbBU0xMWK1_s08pxsu382PoQ574Oy7RNvWYycUmbadbUlL_r0IFxMFWgiQJAyUyYR1O5gCPKlsawMeUjHlhxx5NWUTLn6zkNbk-PXDT8JYETMs/s1600/02_code_01_04.jpeg" height="149" width="320" /></a></div>
Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com0tag:blogger.com,1999:blog-3250011985483393306.post-51756174195125884802015-03-13T18:23:00.001-07:002015-07-02T18:09:30.358-07:00Starting Codes <div style="text-align: justify;">
First of all this is a collection of codes that are found in different tutorials related to Python GUI with <br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiV7QyfHUO-qNaaBTGy8vXZdQ_aAaD4Av9pLi5nDhY5U_uyDo53cxCwVpCyBIIIfR63ZOyfVTSbpk_tkcGJx1VteXd01J5kuKpYkUxMs_a9oL51LlxQnXyMsc3NIwbMAm2od5863-oVDl4/s1600/Tk_logo.png" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiV7QyfHUO-qNaaBTGy8vXZdQ_aAaD4Av9pLi5nDhY5U_uyDo53cxCwVpCyBIIIfR63ZOyfVTSbpk_tkcGJx1VteXd01J5kuKpYkUxMs_a9oL51LlxQnXyMsc3NIwbMAm2od5863-oVDl4/s1600/Tk_logo.png" /></a></div>
TKinter. It's going to be used Python 2.7 and PyScripter, some people preffer other options than Pyscripter, like PyCharm, it's a matter of tastes. There are different ways in wihch one could use TKinter in Python, some are really practical and other are a little bit fancy, of course one must see those pieces of code before telling this or that right away, so let's see some of them.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgue_gWQAIzjV_v6PMEJuRQCXWpxaKxpe7vE3moAZ_VCmdW8xCj-Tt2p3zU49BfF-MCY_Jy6ZtovzwPBEP2v2V2vEqOsFxfC5FD7Jhy7i4H-bUeA-Bd9MwVF0YGFszjfQapNH4Jy14SKNc/s1600/1st_code_article_01.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="95" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgue_gWQAIzjV_v6PMEJuRQCXWpxaKxpe7vE3moAZ_VCmdW8xCj-Tt2p3zU49BfF-MCY_Jy6ZtovzwPBEP2v2V2vEqOsFxfC5FD7Jhy7i4H-bUeA-Bd9MwVF0YGFszjfQapNH4Jy14SKNc/s320/1st_code_article_01.gif" width="320" /></a></div>
</div>
<br />
<a name='more'></a><br />
<br />
<b><span style="color: orange;">The first one </span></b><br />
<div style="text-align: justify;">
is considered not so wise because in the line that import Tkinter, well, this line will import everything from Tkinter and the code could get messy if we are importing from other libraries too, so you get the picture (well, maybe not, if that's the case, you could get a little bit more of information about that in "<a href="https://pythonconquerstheuniverse.wordpress.com/2011/03/28/why-import-star-is-a-bad-idea/">Why import star is a bad idea </a>")</div>
<br />
<blockquote class="tr_bq">
<span style="color: #6aa84f;">from </span>Tkinter <span style="color: #6aa84f;">import *</span><br />
root <span style="color: #6aa84f;">= </span>Tk<span style="color: #6aa84f;">()</span><br />
theLoneLabel <span style="color: #6aa84f;">= </span>Label<span style="color: #6aa84f;">(</span>root, text =<span style="color: #f1c232;"> "About time!"</span><span style="color: #6aa84f;">)</span><br />
theLoneLabel.pack<span style="color: #6aa84f;">()</span><br />
root<span style="color: #6aa84f;">.</span>mainloop<span style="color: #6aa84f;">()</span></blockquote>
<br />
When you run the previous code,<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjATviWI4usxrEKKuiRLB6LI3l5lj0Oa_n-OmgBrHUhAONxGc2tfNkkyrj5qzNoMdZ3RVZAr6ikzcqASkXubcmqN_C3_1DVCOsIj9X7GH54Vp7WRM-8spxUfipEn948GvADsZLcL8KR2Lw/s1600/01_code_01_01.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="120" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjATviWI4usxrEKKuiRLB6LI3l5lj0Oa_n-OmgBrHUhAONxGc2tfNkkyrj5qzNoMdZ3RVZAr6ikzcqASkXubcmqN_C3_1DVCOsIj9X7GH54Vp7WRM-8spxUfipEn948GvADsZLcL8KR2Lw/s1600/01_code_01_01.jpeg" width="320" /></a></div>
<br />
it would just show text in a label inside a little window that TKinter creates,<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7lni9tYCb_N4u3rgNcoAQNDJEkL7jWGBvSr3pAq8SJ5Lt77B16jrr81R94nPUfmIbzfOMJa9zGJGL8tuwleF0YuEoWQwyyf_aL8SgEL-J7-f-mEh899NDVmzUrs2CH5AiPQohd_WibYU/s1600/01_code_01_02.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7lni9tYCb_N4u3rgNcoAQNDJEkL7jWGBvSr3pAq8SJ5Lt77B16jrr81R94nPUfmIbzfOMJa9zGJGL8tuwleF0YuEoWQwyyf_aL8SgEL-J7-f-mEh899NDVmzUrs2CH5AiPQohd_WibYU/s1600/01_code_01_02.jpeg" width="293" /></a></div>
<br />
<br />
<br />
<br />
and notice that the tittle hasn't been edited so we will find that the tittle says "tk", just that two words, that's because we never messed with the tittle properties.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgm7ciV-U3K6z0WtSMUCjWHKuIjiWRH9JZSR0OZ_1BpVpe1U-5OlC-TjTA7FXQoNDA0O7MpvNRla4haw8ilI9smuf1ePp-OoR-LBd9V5-O1y4xNbdB_97Nl5xBqrebh6oRxq8CKv1S_kdw/s1600/01_code_01_03.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgm7ciV-U3K6z0WtSMUCjWHKuIjiWRH9JZSR0OZ_1BpVpe1U-5OlC-TjTA7FXQoNDA0O7MpvNRla4haw8ilI9smuf1ePp-OoR-LBd9V5-O1y4xNbdB_97Nl5xBqrebh6oRxq8CKv1S_kdw/s1600/01_code_01_03.jpeg" /></a></div>
<br />
<span style="color: #cc0000;"></span>
<span style="color: #cc0000;"></span>
<span style="color: #cc0000;"></span><br />
<span style="color: #cc0000;"></span><span style="color: #cc0000;"><b>The second one</b></span><br />
<div style="text-align: justify;">
is considered to be more easy to handle along with other imported libraries because in this case we're just importing parts from Tkinter, just the ones that we need and nothing more.</div>
<br />
<blockquote>
<span style="color: #6aa84f;">from </span>Tkinter <span style="color: #6aa84f;">import </span>Label, mainloop<br />
Label<span style="color: #6aa84f;">(</span>text <span style="color: #6aa84f;">= </span><span style="color: #f1c232;">"About Time!"</span><span style="color: #6aa84f;">).</span>pack<span style="color: #6aa84f;">()</span><br />
mainloop<span style="color: #6aa84f;">()</span></blockquote>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFLbHOecJ5B3oXAMtI-ioR3XxJcpKVyo-rqX3faMBc25E7T_jUgDwNDWUSDAGhORQUMXTyUuk1CoWi3Y2fj3lHac5iXDcfWGHxlyXE-Hos5mLhQFilfxIHsBlp_pl-NrVwIHWoChTP7k8/s1600/01_code_02_01.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="72" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFLbHOecJ5B3oXAMtI-ioR3XxJcpKVyo-rqX3faMBc25E7T_jUgDwNDWUSDAGhORQUMXTyUuk1CoWi3Y2fj3lHac5iXDcfWGHxlyXE-Hos5mLhQFilfxIHsBlp_pl-NrVwIHWoChTP7k8/s1600/01_code_02_01.jpeg" width="320" /></a></div>
<br />
This last code is supposed to do the same than the first one,<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEij6cPc5iO-OTmNidi2zpKGEpOfSAPdCLYwBOlI4MsIbDjuUy6z3j66VDYprxVan_PercC6b_FtduG2rhq672cMPSNWd8N3JEdezQkeTlOlRyaiJW8eWcKHVeYuY6ZvPpkikpRaWQ0vysw/s1600/01_code_02_02.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="139" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEij6cPc5iO-OTmNidi2zpKGEpOfSAPdCLYwBOlI4MsIbDjuUy6z3j66VDYprxVan_PercC6b_FtduG2rhq672cMPSNWd8N3JEdezQkeTlOlRyaiJW8eWcKHVeYuY6ZvPpkikpRaWQ0vysw/s1600/01_code_02_02.jpeg" width="320" /></a></div>
of course there are some similarities, but the last one it's more compact, yeah!<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5lw6dkAipkR0o70lYnW3bxcV-VzZ6TPxwdzYr3uLfDptVl7iBWzFvx3wp_QOORLEyILphkKGsnCdipwm1qNYWBN6nWhN0zq3DM9tz9y72BLHXvts265vH4Qx70G4VKzvso2ud_bw8uDM/s1600/01_code_02_03.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="128" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5lw6dkAipkR0o70lYnW3bxcV-VzZ6TPxwdzYr3uLfDptVl7iBWzFvx3wp_QOORLEyILphkKGsnCdipwm1qNYWBN6nWhN0zq3DM9tz9y72BLHXvts265vH4Qx70G4VKzvso2ud_bw8uDM/s1600/01_code_02_03.jpeg" width="320" /></a></div>
<br />Jenko Stevenshttp://www.blogger.com/profile/08004359888684049518noreply@blogger.com0