Wednesday, November 20, 2019

drop down list using tkinter library in python

try:
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk
def select():
    sf = "value is %s" % var.get()
    root.title(sf)
    # optional
    color = var.get()
    root['bg'] = color
root = tk.Tk()
# use width x height + x_offset + y_offset (no spaces!)
#root.geometry("%dx%d+%d+%d" % (330, 80, 200, 150))
#root.geometry("%dx%d+%d+%d" % (220, 40, 200, 150))
root.title("MM32_MICROCONTROLLER_DATA")
root.configure(background="light green")
var = tk.StringVar(root)
# initial value
var.set('MM32F_SERIES')
choices = ['MM32F_SERIES', 'MM32L_SERIES', 'MM32SPIN_SERIES', 'MM32W_SERIES','MM32P_SERIES']
Fchoices = ['MM32F003', 'MM32L_SERIES', 'MM32SPIN_SERIES', 'MM32W_SERIES','MM32P_SERIES']
option = tk.OptionMenu(root, var, *choices)
option.pack(side='left', padx=1, pady=1)
button = tk.Button(root, text="check value slected", command=select)
button.pack(side='left', padx=20, pady=1)
root.mainloop()

output





matrix list in python

a=[[10,20,30],[25,40,50],[60,70,80]]
print(a)
for r in a:
    print(r)

output:
[[10, 20, 30], [25, 40, 50], [60, 70, 80]]
[10, 20, 30]
[25, 40, 50]
[60, 70, 80]

nested list in python

a=[10,20,[30,40]]
print(a)
print(a[0])
print(a[1])
print(a[2][0])
print(a[2][1])

output:
[10, 20, [30, 40]]
10
20
30
40

clear method in python

clear method is used for to clear entire list ,list elements

a=[10,20,30]
print(a)
a.clear()
print(a)

output:
[10, 20, 30]
[]

list elements check using print

a=[10,20,30]
print(10 in a)
print(100 in a)
print(100 not in a)

output:
True
False
True

comparing the list elements in python


comparing list elements.

x=['abc','bcd','cde']
y=['abc','bcd','cde']
z=['ABC','BCD','CDE']
print(x==y)
print(x==z)
print(x!=z)

output:
True
False
True

example 2:
x=['abc','bcd','cde']
y=['abc','bcd','cde']
z=['ABC','BCD','CDE']
print(x[0] is y[0])
print(x==z)
print(x!=z)

output:
True
False
True

mathematical operation on the list in python

a1=[20,5,16,10,8]
a2=[10, 20, 30, 40]
c=a1+a2
print(c)

output:
[20, 5, 16, 10, 8, 10, 20, 30, 40]

copy method in python

a1=[20,5,16,10,8]
y=a1.copy()
print(a1)
print(y)

output:
[20, 5, 16, 10, 8]
[20, 5, 16, 10, 8]

slice operator using list

list we can copy  by using slice operator  ,the element of the first  list should same in the next list but when you change the element from the cloned list  that elements only changes previous list should same like before.

x=[10,20,30,40]
y=x[:]
print(x)
y[1]=111
print(id(x))
print(id(y))
print(y)
print(x)

output:
[10, 20, 30, 40]
2576163316104
2576163860680
[10, 111, 30, 40]
[10, 20, 30, 40]

list alias in python

In the list we can make many alias for the single list

x1 = [5, 8, 10, 16, 20]
y=x1
print(x1)
print(y)
print(id(x1))
print(id(y))

output:
[5, 8, 10, 16, 20]
[5, 8, 10, 16, 20]
2576163839240
2576163839240

sorting the element of the list in reverse using sort method

a1=[20,5,16,10,8]
a1.sort()
print(a1)
a1.sort(reverse=True)
print(a1)

output:

[5, 8, 10, 16, 20]
[20, 16, 10, 8, 5]

example:
if reverse is false then it will give normal sorting order only
a1=[20,5,16,10,8]
a1.sort()
print(a1)
a1.sort(reverse=True)
print(a1)
a1.sort(reverse=False)
print(a1)

output:
[5, 8, 10, 16, 20]
[20, 16, 10, 8, 5]
[5, 8, 10, 16, 20]

python class topic video