Python Forum
How to place global tk text widget in class or on canvas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to place global tk text widget in class or on canvas
#2
Your DraggableLine class should not know how it is to be used by the application. It should not know that there are multiple instances of draggable line and it should not be putting information in a global variable and it should definitely not know about calculating differences and writing the result in labels. The DraggableLine class should let you drag the line, provide a way to get the location of the line, and have a way to specify a function to be called when the line is moved.

Use Canvas as an example. Canvas does not know anything about DraggableLine, but it provides a way to call DraggableLine.clickonline when the canvas is picked. Your DraggableLine class should have a connect and disconnect for the line moved event. Your application would use this to call a function in the application that knows about all three plots and how to get the cursor/line position from a plot and know about the labels and all the calculations and stuff. If you want to use the DraggableLine information in multiple places you just connect multiple functions to the event.

Since DraggableLine only has one interesting event, you can simplify the connect/disconnect to maintain a list of functions.
class DraggableLine():
    def __init__(self, plot, canvas):
        self.canvas = canvas
        self.callbacks = []
        self.x = 0
        self.cursor = lines.Line2D((self.x, self.x), (-2, 2), color='red', picker=5)
        plot.add_line(self.cursor)
        self.canvas.draw_idle()
        self.canvas.mpl_connect('pick_event', self.pick_callback)

    def connect(self, func):
        self.callbacks.append[func]
        return func # App will use as handle for disconnect

    def disconnect(self, handle):
        self.callbacks.remove(handle)

    def pick_callback(self, event):
        if event.artist == self.cursor:
            self.move = self.canvas.mpl_connect("motion_notify_event", self.move_callback)
            self.release = self.canvas.mpl_connect("button_press_event", self.button_callback)
 
    def move_callback(self, event):
        self.x = event.xdata
        self.cursor.set_xdata([self.x, self.x])
        self.canvas.draw_idle()
 
    def button_callback(self, event):
        self.canvas.mpl_disconnect(self.move)
        self.canvas.mpl_disconnect(self.release)
        for cb in self.callbacks():
            cb()
Somewhere in you application you would write a function to calculate differences and update the results.
def update_cusor_displays():
    a = pageOne.line1.x
    b = pageOne.line2.x
    c = pageOne.line3.x
    tex1Value = str(b-a)
    tex2Value = str(c-a)

pageOne.line1.connect(update_cursor_displays)
pageOne.line2.connect(update_cursor_displays)
pageOne.line3.connect(update_cursor_displays)
Reply


Messages In This Thread
RE: How to place global tk text widget in class or on canvas - by deanhystad - Jul-04-2020, 09:25 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Place QT Window in the middle AlphaInc 10 2,226 Aug-03-2023, 05:40 PM
Last Post: Axel_Erfurt
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,903 Jun-26-2022, 06:26 PM
Last Post: menator01
  [Tkinter] Text widget inert mode on and off rfresh737 5 3,881 Apr-19-2021, 02:18 PM
Last Post: joe_momma
  Line numbers in Text widget rfresh737 3 5,417 Apr-15-2021, 12:30 PM
Last Post: rfresh737
  [Tkinter] canvas widget scroll issue chrisdb 2 3,867 Apr-07-2021, 05:48 AM
Last Post: chrisdb
  tkinter text widget word wrap position chrisdb 6 7,576 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  Label.Place did not work? ATARI_LIVE 15 5,253 Sep-18-2020, 04:22 PM
Last Post: ATARI_LIVE
  [PyQt] remove widget from other class issac_n 2 3,150 Aug-05-2020, 01:55 PM
Last Post: deanhystad
  [Tkinter] Get the last entry in my text widget Pedroski55 3 6,410 Jul-13-2020, 10:34 PM
Last Post: Pedroski55
  [Tkinter] How to place scroll bar correctly scratchmyhead 1 3,954 May-18-2020, 04:17 PM
Last Post: scratchmyhead

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020