Hello, sorry for the rudeness for asking for help on the first post but I'm still a beginner in programming.
I am trying to create a GUI for plotting function using Tkinter and Python. In the top a label will show the current position of the mouse in the canvas and the position of the last click in the canvas. I am learning classes at uni so I need to learn how to use it.
Whenever the label changes, instead of it staying in the top, a new window opens up with the details.
from Tkinter import *
def Click(event):
root = Tk()
x, y = event.x, event.y
PointFrame(root).Clicked(x, y)
def Move(event):
root = Tk()
x, y = event.x, event.y
PointFrame(root).Moved(x, y)
class PointFrame(object):
def __init__(self, root):
self._root = root
CoordRow = Frame(root)
CoordRow.grid(column=0, row=0)
self._LastPointLabel = Label(CoordRow)
self._LastPointLabel.pack(side=LEFT)
self._CurrentCoordLabel = Label(CoordRow)
self._CurrentCoordLabel.pack(side=RIGHT)
def Clicked(self, x, y):
self._x = x
self._y = y
self._LastPointLabel.config(text = 'Last Point Clicked: (' + str(self._x) + ',' + str(self._y) + ')')
def Moved(self, x, y):
self._x = x
self._y = y
self._CurrentCoordLabel.config(text = 'Cursor Point: (' + str(self._x) + ',' + str(self._y) + ')')
class FunctionFrame(object):
def __init__(self, root):
self._root = root
FunctionRow = Frame(root, relief=SUNKEN)
FunctionRow.grid(column=0, row=2)
g1 = Label(FunctionRow, text='Function in X: ')
g1.pack(side=LEFT)
FunctionInXInput = Entry(FunctionRow, width=35)
FunctionInXInput.pack(side=LEFT)
h1 = Label(FunctionRow, text=' Function Colour: ')
h1.pack(side=LEFT)
FunctionColourInput = Entry(FunctionRow, width=20)
FunctionColourInput.pack(side=LEFT)
space = Label(FunctionRow, text=' ')
space.pack(side=LEFT)
i1 = Button(FunctionRow, text='Select', padx = 5, command = CreateFunction())
i1.pack(side=RIGHT)
class PlotFrame(object):
def __init__(self, root):
self._root = root
PlotRow = Frame(root, relief=SUNKEN)
PlotRow.grid(column=0, row=3, pady=20)
a = Label(PlotRow, text='Plot Settings ')
a.pack(side=LEFT)
b1 = Label(PlotRow, text='Start X: ')
b1.pack(side=LEFT)
StartXInput = Entry(PlotRow, width=10)
StartXInput.pack(side=LEFT)
c1 = Label(PlotRow, text=' End X: ')
c1.pack(side=LEFT)
EndXInput = Entry(PlotRow, width=10)
EndXInput.pack(side=LEFT)
d1 = Label(PlotRow, text=' Start Y: ')
d1.pack(side=LEFT)
StartYInput = Entry(PlotRow, width=10)
StartYInput.pack(side=LEFT)
e1 = Label(PlotRow, text=' End Y: ')
e1.pack(side=LEFT)
EndYInput = Entry(PlotRow, width=10)
EndYInput.pack(side=LEFT)
f1 = Label(PlotRow, text=' Steps: ')
f1.pack(side=LEFT)
StepsInput = Entry(PlotRow, width=10)
StepsInput.pack(side=LEFT)
class PlotApp(object):
def __init__(self, root):
self._root = root
PlotFrame(root)
FunctionFrame(root)
PointFrame(root)
self.CreateCanvas()
def CreateCanvas(self):
canvas = Canvas(self._root, bg='White')
canvas.grid(column=0, row=1, sticky=(N, W, E, S))
canvas.bind("<Button-1>", Click)
canvas.bind("<Enter>", Move)
def CreateFunction():
pass
def main():
root = Tk()
app = PlotApp(root)
root.mainloop()
if __name__ == '__main__':
main()
Also could anyone tell me how to find current coordinates of mouse? Right now its if it enters the canvas.
Thanks and I hope to contribute in this site more in the future!