如何用线性方程制作简单的GUI图

时间:2015-07-06 15:01:56

标签: python macos user-interface numpy matplotlib

我是初学程序员。我有一个任务是使用线性方程y=mx+b和一组参数来制作一个GUI,我可以在其中更改m和b值。我有matplotlib和numpy。我也有用于GUI的tkinter。这就是我到目前为止我在坐标GUI上编辑我的朋友代码。

 def onButtonValChange():
    if X1.get() != '':
        x[0] = float(X1.get())

    if Y1.get() != '':
        y[0] = float(Y1.get()


def createGraph(x,y):
    graphRoot = Tk.Tk()
    graphRoot.wm_title("Your Graph")
    graphRoot.resizable(0,0)
    f = Figure(figsize=(5, 4), dpi=100)
    a = f.add_subplot(111)
    a.plot(x, y)
    canvas = FigureCanvasTkAgg(f, master=graphRoot)
    canvas.show()
    canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

x = [1]
y = [1] 
ButtonChangeValues = Tk.Button(root, text="Submit Change", command=onButtonValChange)
ButtonChangeValues.grid(row=11, columnspan=3)

ButtonCreateGraph = Tk.Button(root, text="Create This Graph", command=lambda: createGraph(x, y))
ButtonCreateGraph.grid(row="15", columnspan=3)

Tk.mainloop()

1 个答案:

答案 0 :(得分:0)

所以这里有一些代码,我使用它的移动线图,但你可以调整代码只在你改变方程时移动。所有你需要做的就是使用正确的mx + b公式填充x0Coords y0Coords和xy0Coords的列表

# Run from IDLE or LXTerminal not IDLE 3
# for import spidev to work, must run as python (v2) not python3

from Tkinter import * #to run on python 2, use Tkinter, for python3 use tkinter
import math
from datetime import datetime, timedelta
import numpy as np
import spidev

#--------------------- variables -------------------------
#---user defined settings
screenWidth = 450
resolution = 5 #number of pixels between data points, for visual purposes only
samplePeriod = 100 #milliseconds, time between data points written to txt file
timeRange = .5 #minutes
#---end user settings

baseTime = int(timeRange*60*1000/screenWidth)
startTime = datetime.now()

nl = "\n"
root = Tk()
root.title("Simple GUI")
root.geometry("500x300") #widthxheight

C = Canvas(root, bg = "gray", height = 250, width = screenWidth)


x0Coords = []
y0Coords = []
xy0Coords = []

coordLength = int(screenWidth/resolution)

#---initiation of lists
for i in range(0,coordLength):
    x0Coords.append(i*resolution)
    y0Coords.append(125)
    xy0Coords.append(0)
    xy0Coords.append(0)

#putting X and Y corrdinites in a list
def coordinate():
    global x0Coords, y0Coords, xy0Coords
    for i in range(0,coordLength*2,2):
        xy0Coords[i] = x0Coords[i/2]
        xy0Coords[i+1] = y0Coords[i/2]
    #print(xy0Coords)
#---End initiation of lists


c0 = C.create_rectangle(0,0,20,50)
cl0 = C.create_line(xy0Coords)
pressure = Label(root, text="test")
pressure.pack()

spi_0 = spidev.SpiDev()
spi_0.open(0, 0)

#--------------------------- End of Variables -------------------------

#--------------------------- Definitions ------------------------------

#shifts y values down in index in array to represent time moved forward
def shiftCoords(nextValue):
    global y0Coords, xy0Coords
    y0Coords.pop(0)
    y0Coords.append(nextValue)
    coordinate()

#updates the GUI based on the new time
def move_time():
    global c0,cl0,xy0Coords, resolution, baseTime
    C.delete(c0)
    C.delete(cl0)
    c0 = C.create_rectangle(0,0,20,int(float(readadc_0(0))/1023*250))
    shiftCoords(125-int(float(readadc_0(0))/1023*125))
    cl0 = C.create_line(xy0Coords)
    #print(float(readadc_0(0))/1023*250)
    root.title("V= " + str(round(3.3*float(readadc_0(0))/1023,2)))
    root.after(baseTime*resolution,move_time)

C.pack()
root.after(baseTime,move_time)
root.after(samplePeriod,writeData)
root.mainloop()