Wright State University Lake Campus/2019-1/Python
Importing the math module
edit# Python imports things, like "math"
import math
print("math.pi", math.pi)
#print("this crashes: print(pi)",pi)
from math import pi, sin, cos
print(pi,sin(pi/4),cos(pi/4))
Matrices
editThis is why most Lake students prefer MatLab to Python (though with modules Python would certainly improve upon the following...)
m=[[1,2,3],
[4,5,6],
[7,8,9]]
def transposeMatrix(m):
return map(list,zip(*m))
def getMatrixMinor(m,i,j):
return [row[:j] + row[j+1:] for row in (m[:i]+m[i+1:])]
def getMatrixDeternminant(m):
#base case for 2x2 matrix
if len(m) == 2:
return m[0][0]*m[1][1]-m[0][1]*m[1][0]
determinant = 0
for c in range(len(m)):
determinant += ((-1)**c)*m[0][c]*getMatrixDeternminant(getMatrixMinor(m,0,c))
return determinant
def getMatrixInverse(m):
determinant = getMatrixDeternminant(m)
#special case for 2x2 matrix:
if len(m) == 2:
return [[m[1][1]/determinant, -1*m[0][1]/determinant],
[-1*m[1][0]/determinant, m[0][0]/determinant]]
#find matrix of cofactors
cofactors = []
for r in range(len(m)):
cofactorRow = []
for c in range(len(m)):
minor = getMatrixMinor(m,r,c)
cofactorRow.append(((-1)**(r+c)) * getMatrixDeternminant(minor))
cofactors.append(cofactorRow)
cofactors = transposeMatrix(cofactors)
for r in range(len(cofactors)):
for c in range(len(cofactors)):
cofactors[r][c] = cofactors[r][c]/determinant
return cofactors
###################################
# Program to multiply two matrices using nested loops
# 3x3 matrix
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
# result is 3x4
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
print('printing product')
# iterate through rows of X
for i in range(len(X)):
# iterate through columns of Y
for j in range(len(Y[0])):
# iterate through rows of Y
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for r in result:
print(r)
#####################################
myOut=transposeMatrix(m)
print('transpose\n',list(myOut))
GUI tkinter
editFeb 27
edithttps://docs.python.org/3.6/library/tkinter.html I need to make a gui to facilitate the writing of tests. Now I have a laborious system that uses a csv file that I modify using Excel.
#I hound this hard to understand. It refers to the following code:
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
self.hi_there = tk.Button(self)
self.hi_there["text"] = "Hello World\n(click me)"
self.hi_there["command"] = self.say_hi
self.hi_there.pack(side="top")
self.quit = tk.Button(self, text="QUIT", fg="red",
command=self.master.destroy)
self.quit.pack(side="bottom")
def say_hi(self):
print("hi there, everyone!")
root = tk.Tk()
app = Application(master=root)
app.mainloop()
# A better intro might be at:
#https://python-textbok.readthedocs.io/en/1.0/Introduction_to_GUI_Programming.html
28 February 2019
editimport tkinter as tk
root = tk.Tk()
tk.Label(root,
text='''Does anybody in Phy1060 want
to learn how to make a GUI using Python?''',
fg = "red",
font = "Times").pack()
tk.Label(root,
text="GUI is short for",
fg = "light green",
bg = "dark green",
font = "Helvetica 16 bold italic").pack()
tk.Label(root,
text="Graphical User Interface",
fg = "blue",
bg = "yellow",
font = "Verdana 10 bold").pack()
tk.Label(root,
text='''
See https://www.python-course.eu/python_tkinter.php
A GUI can be used to allow teachers
to use Quizbank quizzes without any knowledge of Python
or advanced programing of any kind. It should be possible
to post questions from a bank by just clicking on buttons.
I wonder if I can do this? Yes, I can skip lines on this GUI
And it is not hard to count seconds:''',
fg = "black",
bg = "white",
font = "Verdana 10").pack()
counter = 0
def counter_label(label):
def count():
global counter
counter += 1
label.config(text=str(counter))
label.after(1000, count)
count()
label = tk.Label(root, fg="green")
label.pack()
counter_label(label)
button = tk.Button(root, text='Stop', width=25, command=root.destroy)
button.pack()
root.mainloop()
Hovering
edit#https://stackoverflow.com/questions/20399243
import tkinter as tk
class Example(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.l1 = tk.Label(self, text="Hover over me")
self.l2 = tk.Label(self, text="", width=40)
self.l1.pack(side="top")
self.l2.pack(side="top", fill="x")
self.l1.bind("<Enter>", self.on_enter)
self.l1.bind("<Leave>", self.on_leave)
def on_enter(self, event):
self.l2.configure(text="Hello world")
def on_leave(self, enter):
self.l2.configure(text="")
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(side="top", fill="both", expand="true")
root.mainloop()