#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Apr 5 11:15:23 2022 @author: richardson """ # python # importing and exporting spreadsheets import pandas as pd import numpy as np import random as rd # let's make a bunch of random data bubba1 = [rd.uniform(0,20) for j in range(1000)] bubba2 = np.reshape(bubba1,(-1,4)) # now make it into a data frame bubbaDF = pd.DataFrame(data=bubba2,columns=['col1','col2','Hi','Mommy']) # now we write to an excel file filenm = "/Users/richardson/Documents/2022S_na/python/bubba.xlsx" bubbaDF.to_excel(filenm,sheet_name="Data",header=None,index=None) # if you want to keep the column names, delete "header=None" # now we write to a csv file filenm = "/Users/richardson/Documents/2022S_na/python/bubba.csv" bubbaDF.to_csv(filenm,header=None,index=None) # Here is how to input a csv or excel file to a list or numpy array #csv filenm = "/Users/richardson/Documents/2022S_na/python/bubba.csv" WS = pd.read_csv(filenm, header=None) bubbaNumpy = np.array(WS) # makes a numpy array if you want that #excel filenm = "/Users/richardson/Documents/2022S_na/python/bubba.xlsx" WS = pd.read_excel(filenm, sheet_name="Data", header=None) bubbaList = WS.values.tolist() # makes it a 2-d list / array # Now this is how to make a GUI in python # how to make a GUI # close windows and start a new console before running this again # this is a function to calculate a list of primes through m def actualps(m): if type(m)!=int or m<=1: return "This is a "+str(type(m)).replace('','')+" and is not a positive integer >=2." answer = [2] for j in range(3,m): #for each j, we'll just make a list counting all products of all prime divisors # and then check if it's nonempty hubba = [42 for a in answer if j%a==0] # we now want to add [j] if hubba is empty, otherwise add [] answer += [] if hubba else [j] # if [list] is like 'if true' when [list] is nonempty, otherwise like 'if false' return answer # Now we make the GUI import tkinter as tkr from PIL import ImageTk, Image window = tkr.Tk() window.attributes("-topmost", True) window.title("Bubba's Primes") window.geometry("600x400") lbl = tkr.Label(window,text="These R Bubba's primes. Use them well.", font=("Comic Sans MS",24)) lbl.grid(column=0,row=0) txt = tkr.Entry(window,width=10) txt.grid(column=0, row=1) def whenYouClick(): result = "The primes <= "+txt.get() second = "are " ourNumber = int(txt.get()) ourPrimes = actualps(ourNumber) for n in range(0,len(ourPrimes)-1): second += str(ourPrimes[n])+", " if n%10==9: second += "\n" second += str(ourPrimes[len(ourPrimes)-1]) lbl2.configure(text = result) lbl3.configure(text = second, justify=tkr.LEFT) friend = tkr.Button(window, text="Click to get Bubba's primes", command = whenYouClick ) friend.grid(column=0,row=2) lbl2 = tkr.Label(window,text="", font=("Comic Sans MS",18)) lbl2.grid(column=0,row=3) lbl3 = tkr.Label(window,text="", font=("Comic Sans MS",18)) lbl3.grid(column=0,row=4) # this part displays an image img1 = Image.open("/Users/richardson/Documents/2022S_na/python/monkey.png") img = ImageTk.PhotoImage(img1) panel = tkr.Label(window, image=img) panel.grid(column=0, row=5) window.mainloop() #### # # Do this to put a graph in the window # we will make a second window import matplotlib matplotlib.use('TkAgg') import numpy as np from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.figure import Figure window2= tkr.Tk() fig = Figure(figsize=(6,6)) a = fig.add_subplot(221) x = np.linspace(0,np.pi,100) funct = np.sin(x) a.plot(x,funct,color='blue') a.set_title ("sample plot", fontsize=16) a.set_ylabel("y", fontsize=14) a.set_xlabel("x", fontsize=14) b = fig.add_subplot(224) x = np.linspace(0,np.pi,100) funct = np.cos(x) b.plot(x,funct,color='red') b.set_title ("other plot", fontsize=16) b.set_ylabel("y", fontsize=14) b.set_xlabel("x", fontsize=14) canvas = FigureCanvasTkAgg(fig, master=window2) canvas.get_tk_widget().pack() canvas.draw() def onclick(event): #this happens when you click on the graph global point point = [event.xdata, event.ydata] print(point) fig.canvas.mpl_connect('button_press_event', onclick) #this connects the canvas with the event handler window2.mainloop()