Home > front end >  How to configure/edit objects created in class?
How to configure/edit objects created in class?

Time:09-14

I am making a program in python using tkinter with someting like sticky notes that i can move, but now i want to be able to add text to these notes using an Entrybox and a Button but I dont know how to configure the text that was before created in Label that was made using class. I am looking for some solution how to do it if its even possible. Thats my code:

Edit: Ok so i'll try to make it clear

Here's my whole code:

from tkinter import *
import random

class Note:
    

    def __init__(self,master,title,text,color,x_coords,y_coords):

        
        myLabel = Label(master, text=str(title "\n" "-" text),font=("Candara Light",20),height=10,width=13,background=color,anchor=N,wraplength=200)  
        myLabel.place(x=x_coords,y=y_coords)

        
        myLabel.bind("<Button-1>",self.drag_start)
        myLabel.bind("<B1-Motion>",self.drag_motion)

        
    def drag_start(self,event):
        widget = event.widget
        widget.startX = event.x
        widget.startY = event.y

    def drag_motion(self,event):
        widget = event.widget
        x = widget.winfo_x() - widget.startX   event.x
        y = widget.winfo_y() - widget.startY   event.y
        widget.place(x=x,y=y)
    
        
    

def add_text():
    pass

def new_note():
    Note(canvas_frame,add_new_note_title.get(),"",random.choice(note_colors),100,100)


def do_something(event):
    print("You did a thing! "   str(event.x)   ","   str(event.y))

def drag_start(event):
    widget = event.widget
    widget.startX = event.x
    widget.startY = event.y

def drag_motion(event):
    widget = event.widget
    x = widget.winfo_x() - widget.startX   event.x
    y = widget.winfo_y() - widget.startY   event.y
    widget.place(x=x,y=y)

window = Tk()
window.title("To Do")
window.resizable(False,False)

window_width = 900
window_height = 900
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()

x = int((screen_width/2) - (window_width/2))
y = int((screen_height/2) - (window_height/2))

window.geometry(f"{window_width}x{window_height} {x} {y}")


blank_label_top = Canvas(window,background="#cbd1cc",width=900,height=70,highlightthickness=0)
blank_label_top.pack(anchor=N)
blank_label_left = Canvas(window,background="#cbd1cc",width=70,height=900,highlightthickness=0)
blank_label_left.pack(anchor=W)

entry = Entry(window,font=("Arial",20),width=25)
entry.place(x=410,y=17)
submit_button = Button(window,text="Add Text",height=2,width=7,font=("Arial"),command=add_text)
submit_button.place(x=800,y=11)

titles = ['Tekst_1','Tekst_2',"Tekst_3"]
title_name = StringVar()
title_name.set("Tekst_1")

title_chooser = OptionMenu(window,title_name,*titles,command=add_text)
title_chooser.place(x=325,y=22)

add_new_note_button = Button(window,text=" ",font=("Arial",20),height=1,width=3,background="#cbd1cc",activebackground="#cbd1cc",command=new_note)
add_new_note_button.place(x=230,y=10)

add_new_note_title = Entry(window,width=8,font=("Arial",15))
add_new_note_title.insert(0,"New Title")
add_new_note_title.place(x=120,y=25)

note_colors = ["#d9c389","#53d4c5","#53678a","#d9a9d3","#eef5b3","#a13e2f"]

canvas_frame = Frame(window,highlightthickness=0)
canvas_frame.place(x=70,y=70)

canvas = Canvas(canvas_frame,height=830,width=830,background="#66a9ad",highlightthickness=0)
canvas.pack()

#canvas.create_rectangle(25,25,300,400,fill="pink")




label = Label(canvas,bg="red",width=10,height=5)
#label.place(x=100,y=100)

label.bind("<Button-1>",drag_start)
label.bind("<B1-Motion>",drag_motion)

window.bind("<Button-3>",do_something)

Note_1 = Note(canvas_frame,"Tekst","tekst","#c9a1d1",50,50)
Note_2 = Note(canvas_frame,"Tekst2","tekst2","#3ab5de",290,50)
Note_3 = Note(canvas_frame,"Tekst3","tekst3","#90e8a7",290 240,50)


window.mainloop()

ill try to explain this in linked image

https://i.stack.imgur.com/WiIZ8.png

CodePudding user response:

I'm not exactly sure what the rest of your code looks like, but if you're just trying to accept text input and then display it, something like this might work:

    from tkinter import *
def addtxt():
  note = note_inp.get()
  note_disp.insert(0,note)

note_inp = Entry()
note_inp.pack()

ws = Tk()
ws.title('notes')
ws.geometry('200x100')

note_disp = Entry(ws)

note_disp.pack()

btn = Button(
    ws,
    text='add note',
    command = addtxt)
btn.pack()

CodePudding user response:

To access and/or modify data that is part of a class from outside the class, it needs to be made an instance variable of the class.

For example, if you want to modify myLabel from outside the class you first need to make it an instance variable of the class by using the self. prefix.

class Note:
    def __init__(self,master,title,text,color,x_coords,y_coords):
        ...
        self.myLabel = Label(master, ...)
        ...

Next, you can access the attribute by referencing it from the instance of the class, like so:

Note_1 = Note(canvas_frame,"Tekst","tekst","#c9a1d1",50,50)
...
Note_1.myLabel.configure(text="hello, world")

Note: this isn't something unique to tkinter. This is a fundamental aspect of python programming. For more information see Classes in the official Python documentation.

  • Related