How to make simple GUI graph with Linear Equation - python

I am a beginner programmer. I have a task to make a GUI with a linear equation y=mx+b and a set of parameters where I can change the m and b values. I have both matplotlib and numpy. I also have tkinter for the GUI. This is what i have so far i edited my friends code on a coordinate 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()

So here is some code that i use its a moving line graph but you could adapt the code to move only when you change the equation. all you would have to do is populate the lists of x0Coords y0Coords and xy0Coords with the right mx+b formula
# 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()

Related

Matlibplot inside of a thread alongside Tkinter

Error inside of the terminal.
/Users/aidan/Documents/GitHub/Perceptual/Threading.py:81: UserWarning: Starting a Matplotlib GUI outside of the main thread will likely fail.
plt.show()
2022-11-06 14:28:07.960 Python[25376:1206036] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow drag regions should only be invalidated on the Main Thread!'
Code of the python project.
import time
import tkinter as tk
#import tkinter as tkk
from tkinter.ttk import *
from PIL import Image, ImageTk
import random
import threading
#imports for BackEnd
import pyautogui as mouse
import matplotlib.pyplot as plt
#Created Global Array
x = []
y = []
root = tk.Tk()
root.title("Perceptual Threading")
root.geometry("900x600")
#Sperates the canvase into diffrent coloms
root.columnconfigure(6, weight = 0)
root.rowconfigure(10, weight = 0)
def BackEndDebug():
DebugInt = 0
while(DebugInt != -1):
DebugNumber.config(text=f'Random Number: {DebugInt}')
DebugInt += 1
time.sleep(0.0000000001)
#Load Logo
def LoadLogo():
logo = Image.open("PerceptualLogoTransparent.png")
logo = logo.resize((125, 125))
logo = ImageTk.PhotoImage(logo)
logo_label = tk.Label(image = logo)
logo_label.image = logo
logo_label.grid(column=0, row=0, sticky=tk.W, padx=5, pady=5)
def LoadDashboard():
Dashboard = Image.open("Mercades Dashboard .jpeg")
Dashboard = Dashboard.resize((775,300))
Dashboard = ImageTk.PhotoImage(Dashboard)
Dashboard_label = tk.Label(image = Dashboard)
Dashboard_label.image = Dashboard
Dashboard_label.grid(column = 2, row = 6, columnspan=3, rowspan=2)
def MouseGraph():
control = True
count = 0
t = 1
inc = .1
check1, check2, check3 = 0, 1, 2
mouse.moveTo(960, 540)
while control:
thing = mouse.position()
y.append(thing[0] - 960)
x.append(inc * t)
if thing[1] > 540 or thing[1] < 540:
mouse.moveTo(thing[0], 540)
count += 1
t += 1
if count > 2:
if (y[check2] < y[check1] and y[check2] < y[check3]) or (y[check2] == y[check1] and y[check2] < y[check3]):
print(f'{y[check2]} is the relative minimum.')
if (y[check2] > y[check1] and y[check2] > y[check3]) or (y[check2] == y[check1] and y[check2] > y[check3]):
print(f'{y[check2]} is the relative maximum.')
check1 += 1
check2 += 1
check3 += 1
if count > 100:
control = False
# return(x, y)
plt.plot(x, y)
plt.xlabel('time')
plt.ylabel('distance turned')
plt.show()
time.sleep(inc)
DebugNumber = Label(root, text="Debug Number!")
DebugNumber.grid(column= 6, row= 0, sticky=tk.E)
StartBackendDebug = Button(root, text="Start the BackendDebug", command = threading.Thread(target=BackEndDebug).start)
StartBackendDebug.grid(column= 5, row= 0, sticky=tk.E)
StartGraph = Label(root, text="Start Graph!")
StartGraph.grid(column= 6, row= 1, sticky=tk.E)
StartGraphButton = Button(root, text="Begin", command = threading.Thread(target=MouseGraph).start)
StartGraphButton.grid(column= 5, row= 1, sticky=tk.E)
LoadLogo()
LoadDashboard()
root.mainloop()
I have tried returning the arrays after making them global variables but I do not know how to then have a call after the thread is over. This is my first project using threading and am unsure if I need to add classes or what. Is there a way to call the method of a class from another class or run the code in the main thread without Tkinter having an issue. I have also tried looking into other graphing methods but this seems to be the only real way.

How to add tkinter progress bar indeterminate with function

from tkinter import *
from tkinter import ttk
def DOTEST_GUI():
GUI = Tk()
w = 1920
h = 1080
ws = GUI.winfo_screenwidth()
hs = GUI.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
GUI.geometry(f'{w}x{h}+{x:.0f}+{y:.0f}')
def start_p():
progress.start(5)
def stop_P():
progress.stop()
def print_cf(event = None):
import time
print('s')
start_p()
time.sleep(5)
stop_P()
B_TEST = ttk.Button(GUI, text = "test", width = 15, command = print_cf)
B_TEST.pack()
progress = ttk.Progressbar(GUI, orient = HORIZONTAL, length = 100, mode = 'indeterminate')
progress.pack(pady = 10)
GUI.bind("<Return>", print_cf)
GUI.focus()
GUI.mainloop()
DOTEST_GUI()
follow this code progress bar is not running properly.
I tried to remove stop_P(), it's work after 5 second of time.sleep(5).
I would like it to start running progress 5 second until the stop_P() code.
If you want to run a Progressbar for a set amount of time and then stop it, you could do something like this. Consolidate the print_cf function to start and stop the progress and set a range of 5, sleep for 1 second in between and then stop the Progressbar. Placing it on a thread would allow you to do something more time consuming than sleeping one second and printing something.
from tkinter import *
from tkinter import ttk
import time
import threading
def DOTEST_GUI():
GUI = Tk()
w = 1920
h = 1080
ws = GUI.winfo_screenwidth()
hs = GUI.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
GUI.geometry(f'{w}x{h}+{x:.0f}+{y:.0f}')
def run_thread():
execute_thread = threading.Thread(target=print_cf)
execute_thread.start()
def print_cf(event = None):
progress.start()
print('s')
for i in range(5):
print(i)
time.sleep(1)
if i ==4:
progress.stop()
B_TEST = ttk.Button(GUI, text = "test", width = 15, command = run_thread)
B_TEST.pack()
progress = ttk.Progressbar(GUI, orient = HORIZONTAL, length = 100, mode = 'indeterminate')
progress.pack(pady = 10)
GUI.bind("<Return>", print_cf)
GUI.focus()
GUI.mainloop()
DOTEST_GUI()

Tkinter - bouncing icon script

I'm writing a simple bouncing icon program (Python 3.7, Windows 10 x64) to get the feel for Tkinter and canvases. I've posted my code below. My problem with the program is that it clips the edges of the icon (in the direction of motion). If I slow the motion down a bit (by increasing the value in the after method) it no longer clips, but the motion is choppy. Maybe I'm overthinking this, it basically does what I've aimed for. But if this were a game or other project that mattered, how would this be prevented?
from tkinter import *
import os
from PIL import Image, ImageTk
xinc, yinc = 5, 5
def load_image(width, height, imgpath):
loadimg = Image.open(imgpath)
pwid, phi = loadimg.size
pf1, pf2 = 1.0*width/pwid, 1.0*height/phi
pfactor = min([pf1, pf2])
pwidth, pheight = int(pwid*pfactor), int(phi*pfactor)
loaded = loadimg.resize((pwidth, pheight), Image.ANTIALIAS)
loaded = ImageTk.PhotoImage(loaded)
return loaded
def bounce():
global xinc
global yinc
cwid = int(dash.cget('width'))
chi = int(dash.cget('height'))
x = dash.coords(dashposition)[0]
y = dash.coords(dashposition)[1]
if x > cwid-10 or x < 10:
xinc = -xinc
if y > chi-10 or y < 10:
yinc = -yinc
dash.move(dashposition, xinc, yinc)
dash.after(15, bounce)
root = Tk()
root.configure(bg='black')
dash = Canvas(root, bg='black', highlightthickness=0, width=400, height=300)
dash.grid(row=0, column=0, padx=2, pady=2)
imagepath = os.getcwd() + '/img/cloudy.png'
image = load_image(20, 20, imagepath)
x, y = 10, 10
dashposition = dash.create_image(x, y, anchor=CENTER, image=image, tags=('current'))
bounce()
root.mainloop()
cloudy.png
There are two contributing factors to the clipping. The main problem is that load_image(20, 20, imagepath) will only result in a 20x20 object if the original image is square. But your cloud object isn't square. And your border collision calculations will only work if the rescaled cloud object is 20x20. So we need to modify that. The other issue is that you aren't compensating for the Canvas's border. The easy way to do that is to set it to zero with bd=0.
Its usually a good idea during GUI development to use various colors so you know exactly where your widgets are. So that we can more easily see when the cloud hits the Canvas border I've set the root window color to red. I also increased the .after delay, because I just couldn't see what was happening at the speed you set it at. ;) And I made the cloud a bit bigger.
I've made a couple of other minor changes to your code, the main one being that I got rid of that "star" import, which dumps over 100 Tkinter names into your namespace.
Update
I've reduced xinc & yinc to 1, and improved the bounce bounds calculation. (And incorporated jasonharper's suggestion re cwid & chi). I'm no longer seeing any clipping on my machine, and the motion is smoother. I also reduced the Canvas padding to 1 pixel, but that should have no effect on clipping. I just tried it with padx=10, pady=10 and it works as expected.
import tkinter as tk
import os
from PIL import Image, ImageTk
def load_image(width, height, imgpath):
loadimg = Image.open(imgpath)
pwid, phi = loadimg.size
pf1, pf2 = width / pwid, height / phi
pfactor = min(pf1, pf2)
pwidth, pheight = int(pwid * pfactor), int(phi * pfactor)
loaded = loadimg.resize((pwidth, pheight), Image.ANTIALIAS)
loaded = ImageTk.PhotoImage(loaded)
return loaded, pwidth // 2, pheight // 2
xinc = yinc = 1
def bounce():
global xinc, yinc
x, y = dash.coords(dashposition)
if not bx <= x < cwid-bx:
xinc = -xinc
if not by <= y < chi-by:
yinc = -yinc
dash.move(dashposition, xinc, yinc)
dash.after(5, bounce)
root = tk.Tk()
root.configure(bg='red')
dash = tk.Canvas(root, bg='black',
highlightthickness=0, width=800, height=600, bd=0)
dash.grid(row=0, column=0, padx=1, pady=1)
cwid = int(dash.cget('width'))
chi = int(dash.cget('height'))
imagepath = 'cloudy.png'
size = 50
image, bx, by = load_image(size, size, imagepath)
# print(bx, by)
dashposition = dash.create_image(bx * 2, by * 2,
anchor=tk.CENTER, image=image, tags=('current'))
bounce()
root.mainloop()

stop tkinter mainloop disrupting matplotlib

hi I'm a student and I'm working with ktinker for the first time. I am currently trying to create a program that follows this order:
open a map in matplotlib
a popup window is called for users to enter in information
when the user clicks on the map grid the coordinates and user
info are stored in a dictionary
matplotlib generates a plot point on the location
The trouble is, when I call the popup window I have written in tkinter, calling the mainloop() function breaks the matplotlib window and it wont generate the plot points until the map window is closed.
getting rid of the mainloop fixes it but then means i cant get the data from the popup.
Anyone know how I can stop one from interfering with the other? i have to keep them in separate classes as Im meant to demonstrate modularity in my code.
here is the main code
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import popup1 as ap
__author__ = "k1221169"
__date__ = "$15-Nov-2015 11:29:21$"
markerList = []
xplots = []
yplots = []
desc = ''
title = ''
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.random.rand(10))
img = mpimg.imread('overland map 1.png')
imgplot = plt.imshow(img)
plt.ylim(ymax=1)
plt.xlim(xmin=1)
def mrkApnd(a,b):
a.update(b)
print a
markerList.append(a)
def onclick(event):
if spacecheck(event.xdata, event.ydata):
#print markerList[0]['x']
idx = np.abs(xplots - event.xdata).argmin()
print xplots[idx]
for i in markerList:
if xplots[idx] == i['x'] :
print i
#c = i
ap.useroutput(i)
else:
input1 = ap.userinput()
input2 = {'x':event.xdata, 'y':event.ydata}
input1['title'] = title
input1['desc'] = desc
mrkApnd(input1,input2)
drawMarks()
print input1
return markerList
cid = fig.canvas.mpl_connect('button_press_event', onclick)
def drawMarks():
plt.ion()
for i in markerList:
xplots.append(i['x'])
yplots.append(i['y'])
plt.plot(i['x'], i['y'], i['type'])
def spacecheck(x,y):
a = bool
if np.isclose(xplots, x, atol=50.0).any() and np.isclose(yplots, y, atol=50.00).any():
a=True
print 'yes'
return a
plt.draw()
plt.show()
and here is the popup code called from another file
from Tkinter import *
class popup1():
def __init__(self):
pass
def userinput():
pop = Toplevel()
pop.title("marker")
pop.geometry("300x500+200+200")
#string for title
frame = Frame(pop)
entry = Entry(frame)
entry.pack(side = TOP)
frame.pack( padx =20, pady =20)
#radius button for visibility
frame2 = Frame(pop)
selection = StringVar()
radio_1 = Radiobutton(frame2, text = 'Character', variable = selection, value = 'ob')
radio_2 = Radiobutton(frame2, text = 'Item', variable = selection, value = 'or')
radio_3 = Radiobutton(frame2, text='Event', variable = selection, value = 'oy')
radio_1.select()
radio_1.pack(side = LEFT)
radio_2.pack(side = LEFT)
radio_3.pack(side = LEFT)
frame2.pack(padx =30, pady =30)
#radius button for marker type
frame3 = Frame(pop)
visible = bool
check_1 = Checkbutton(frame3, text = 'GM Only', variable = visible, onvalue= True, offvalue= False)
check_1.pack(side = LEFT)
frame3.pack(padx =30, pady =30)
#string for user input
frame4 = Frame(pop)
entry4 = Entry(frame4)
entry4.pack(side = LEFT)
frame4.pack( padx =20, pady =20)
def infoPass():
#info1 = {'title': entry.get(), 'type': selection.get(), 'vis': visible, 'Desc': entry4.get()}
#info.update(info1)
#print info
pop.destroy()
#buttons
label = Label(pop, text="", height=0, width=100)
label.pack()
b = Button(pop, text="Cancel", width=20, command= pop.destroy )
b.pack(side='bottom',padx=5,pady=5)
b2 = Button(pop, text="Save", width=20, command= infoPass )
b2.pack(side='bottom',padx=5,pady=5)
info = {'title': entry.get(), 'type': selection.get(), 'vis': visible, 'desc': entry4.get()}
pop.mainloop()
return info
If i underdstood your question right, then try to add your own loop.
Remove pop.mainloop()
Make the code be a class for a cleaner code
class userinput:
def __init__(self):
#open your window here and style it etc. and:
self.data_sent = False
def infopass(self):
self.data_sent = True
#your infopass code here and
Create your own loop at the end of init:
def __init__(self):
#...
while self.data_sent == False:
root.update()
while data_sent == False:
pop.update()
Call your popup by
mypopup = userinput()
Good luck ;)

How do I refresh an image on TKinter

My function mapupdater isn't working. It seems tkinter isn't keeping a reference to the image. I tried storing the reference in an array, I tried what was suggester here How to update the image of a Tkinter Label widget?
Both don't work.
#-----Begin Global Declarations-----#
global futureonoff
futureonoff = True
#-----End Global Declarations-----#
#Create a new window
window = Tkinter.Tk()
window.wm_title("SpaceMass")
window.geometry("1000x1000")
window.self= Text(bg='black')
window.self.pack(fill="both", expand=True)
#updating map based on ISS location
def mapupdater():
global futureonoff
marker_list = []
timenowforcomputing = strftime("%Y-%m-%d %H:%M:%S", gmtime())
iss.compute(timenowforcomputing)
currentlong = iss.sublong
currentlat = iss.sublat
currentlongfloat= float(iss.sublong)
currentlatfloat= float(iss.sublat)
#convert radians to degrees with the equations 1 radian = 57.2957795 degrees
#TODO Learn how to use pi in python
currentlongfloat = round(currentlongfloat*57.2957795, 3)
currentlatfloat= round(currentlatfloat*57.2957795, 3)
print(currentlongfloat)
print(currentlatfloat)
if futureonoff == True:
futureintermenter = 0
while futureintermenter < len(long_list_3_orbits):
marker_list.append("markers=size:mid|label:F|color:blue|"+str(lat_list_3_orbits[futureintermenter])+","+str(long_list_3_orbits[futureintermenter])+"|")
futureintermenter = futureintermenter + 1
marker_list.append("markers=size:mid|label:S|color:red|"+str(currentlatfloat)+","+str(currentlongfloat)+"|")
toopenupdater = get_static_google_map("mymap2", center="42.950827,-122.108974", zoom=1, imgsize=(500,500), imgformat="gif", maptype="satellite", markers=marker_list)
#Code from https://stackoverflow.com/questions/6086262/python-3-how-to-retrieve-an-image-from-the-web-and-display-in-a-gui-using-tkint
uupdater = urllib.urlopen(toopenupdater)
raw_data_u = uupdater.read()
u.close()
b64_data_u = base64.encodestring(raw_data_u)
imgtoprint = Tkinter.PhotoImage(data=b64_data)
# from http://www.daniweb.com/software-development/python/threads/79337/putting-an-image-into-a-tkinter-thingy
# pick an image file you have .bmp .jpg .gif. .png
# load the file and covert it to a Tkinter image object
panel1.configure(image = imgtoprint)
panel1.image = imgtoprint
#updata map after 30 seconds
window.after(30000, mapupdater)
def togglemap():
global futureonoff
print(futureonoff)
if futureonoff == True:
futureonoff = False
else:
futureonoff = True
mapupdater()
print(futureonoff)
#text_file.configure(text=testing)
#Info about buttons http://effbot.org/tkinterbook/button.htm
#Parsing code from https://stackoverflow.com/questions/773797/updating-tkinter-labels-in-python
#settings for font, font size, pixel size, of the text in our GUI
#convert radians to degrees with the equations 1 radian = 57.2957795 degrees
#TODO Learn how to use pi in python
currentlongfloat = round(currentlongfloat*57.2957795, 3)
currentlatfloat= round(currentlatfloat*57.2957795, 3)
if futureonoff == True:
futureintermenter = 0
while futureintermenter < len(long_list_3_orbits):
marker_list.append("markers=size:mid|label:F|color:blue|"+str(lat_list_3_orbits[futureintermenter])+","+str(long_list_3_orbits[futureintermenter])+"|")
futureintermenter = futureintermenter + 1
marker_list.append("markers=size:mid|label:S|color:red|"+str(currentlatfloat)+","+str(currentlongfloat)+"|")
#places map into GUI
toopen = get_static_google_map("mymap2", center="42.950827,-122.108974", zoom=1, imgsize=(500,500), imgformat="gif", maptype="satellite", markers=marker_list)
#im = PIL.Image.open("mymap2.png")
#imageFile = "mymap2.png"
#Code from https://stackoverflow.com/questions/6086262/python-3-how-to-retrieve-an-image-from-the-web-and-display-in-a-gui-using-tkint
#print(toopen)
u = urllib.urlopen(toopen)
raw_data = u.read()
u.close()
b64_data = base64.encodestring(raw_data)
global imgtoprint
imgtoprint = Tkinter.PhotoImage(data=b64_data)
panel1 = Tkinter.Label(window, image=imgtoprint, bg='black')
panel1.pack(side='top', fill='both', expand='yes')
panel1.place(x=250, y=115)
b = Button(window, text="Browse for XML File", font=("Helvetica", 15), command=fileback, bg = 'black')
b.pack()
b.place(x=425,y=650)
c = Button(window, text="Toggle Orbit Prediction on Map", font=("Helvetica", 15), command=togglemap, bg = 'black')
c.pack()
c.place(x=425,y=850)
mapupdater()
window.mainloop()

Categories

Resources