Home > Back-end >  Trouble with Tkinter coordinates and intialization using place()
Trouble with Tkinter coordinates and intialization using place()

Time:11-04

Very new to tkinter and came across my first major roadbloack. I have a list of labels that is follows around a parent label. I have a function that displays all of these labels based off the coordinates of this parent. Right now the coordinates for the parent label are set at 0,0 no matter where I place it.

Here is the initialization and call of DisplayUnits:

#operations
OperationsChildren = []
templabel1 = Label(MyCanvas, text = "Operations", font=("Arial Bold", 12))
templabel1.place(x = 30, y = 0,)
Operations = CreateSection("Operations", OperationsChildren, None, OperationsList)
Operations.HeaderLabel = ObjectLabel("Operations", templabel1, Operations, MouseObject)
Operations.ChildLabels = DisplayUnits(Operations, MyCanvas, MouseObject)

#medical
MedicalChildren = []
templabel2 = Label(MyCanvas, text = "Medical", font=("Arial Bold", 12))
templabel2.place(x =200, y = 50,)
Medical = CreateSection("Medical", MedicalChildren, None, MedicalList)
Medical.HeaderLabel = ObjectLabel("Medical", templabel2, Medical, MouseObject)
Medical.ChildLabels = DisplayUnits(Medical, MyCanvas, MouseObject)

And here is my display function

def DisplayUnits(Section, Frame, DebugLabel):
    #remove labels inside of sections current label list
    for x in Section.ChildLabels:
        x.label.destroy()
    #initialize label list that will be populated by the new labels
    LabelList = []
    #get initial x and y coordinates
    startX = Section.HeaderLabel.label.winfo_x()
    startY = Section.HeaderLabel.label.winfo_y()
    print("start values are "   str(startX)   ", "   str(startY))
    #set an iterator which will determine how far apart each label is spaced
    SpaceIterator = startY   20
    for index, x in enumerate(Section.SecUnitList): #indexing not needed or used yet
        tempName = x.name
        tempLabel = Label(Frame, text = tempName, font=("Arial Bold", 12))
        #set the location equal to the startx and the iterator which is the advancing Y value
        tempLabel.place(x=startX,y=SpaceIterator)
        #create ObjectLabel class using templabel,
        UnitLabel = ObjectLabel(tempName, tempLabel, None, DebugLabel)
        
        
        LabelList.append(UnitLabel)
        print("Display Iterator is at "   str(SpaceIterator)    " for the unit "  tempName)
        SpaceIterator = SpaceIterator 20 #create label one row below last row
        
    return LabelList

this is what I am seeing this is what I am seeing

this is what I want and am expecting to happen this is what I want

changing to pack and grid based for the parent label. Adding debugs that show the header label location is considered to be at 0,0 even though it is not. I think it might have to do with local vs global positioning but don't know where to even start on checking that.

CodePudding user response:

While I don't normally recommend using place, you can achieve what you want by place's ability to do relative placement. Since we can't reproduce your problem from the code snippets you've presented, here's a simple example that illustrates the concept.

The key is using the in_ parameter which defines what the relative coordinates are relative to, and the relx (relative x) and rely (relative y) parameters. It also sets bordermode to "outside", which means that the relative coordinates are relative to the widget edges rather than the interior portions of the widget.

import tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root, background="blue")
canvas.pack(fill="both", expand=True)

medical_label = tk.Label(canvas, text="Medical")
r2_label = tk.Label(canvas, text="R2")
r3_label = tk.Label(canvas, text="R3")

medical_label.place(x=100, y=100)
r2_label.place(in_=medical_label, relx=0.0, rely=1.0, bordermode="outside")
r3_label.place(in_=r2_label, relx=0.0, rely=1.0, bordermode="outside")
root.mainloop()

screenshot

  • Related