1

I am a very new coder / python user and I don't really understand much. I am trying to understand classes and right now and my program is divided into two of them. I am trying to program a sort of "notepad" and I would like to have a "font changer menu". My main window and my "font changer menu" are in separate classes.

My problem is that I am trying to edit the Text Area(change its font), from the "font changer menu", but they are in separate classes. You can see I have tried putting the font_chooser command inside the "font changer menu" class, but I am obviously told that Our_font is not defined yet, because it is in the other class and therefore cannot edit it with the command. Suggestions on how I should approach something like this?

class fontchangerframe:

    def __init__(self):


    def font_chooser(e):
        self.our_font.config(family=fontbox.get(fontbox.curselection()))

    self.fontchanger = Toplevel()
    self.fontchanger_frame = Frame(self.fontchanger)
    self.fontchanger_frame.pack()

    self.fontbox = Listbox(self.fontchanger_frame, selectmode=SINGLE, width=80)
    self.fontbox.pack()

    #Add font families to Listbox
    for f in font.families():
        self.fontbox.insert('end', f)

    # Bind the list box
    self.fontbox.bind("<ButtonRelease-1>", font_chooser)



class frontpageframe:

    def __init__(self, location):

    self.frame = Frame(location, bg="IndianRed3", width=600, height=600)
    self.frame.pack()
    #Freeze frame in place
    self.frame.grid_propagate(False)


    # Text area
    self.our_font = font.Font(family="Helvetica", size="20")
    self.textEditor = Text(self.frame, font=self.our_font)
    self.textEditor.grid(row=1, column=1)

    # Toolbar
    self.toolbarFrame = Frame(self.frame)
    self.toolbarFrame.grid(row=0, column=0, pady=5)

    # button to open the fontchanger menu
    self.font_skift = Button(self.toolbarFrame, text="Font", command=fontchangerframe)
    self.font_skift.grid(row=0, column=1)
Eric Jin
  • 3,836
  • 4
  • 19
  • 45
  • Does this answer your question? [How would I access variables from one class to another?](https://stackoverflow.com/questions/19993795/how-would-i-access-variables-from-one-class-to-another) – Vlad Nitu Aug 04 '22 at 22:43
  • @VladNitu Im sure that the solution is somewhere to be found in what you suggested. However, I am unable to solve it still.. Would you help me apply the same logic from the answer you have suggested on to my program. Everything works in seperate scripts, it is just when i put them together, that "out font" is not defined yet. Ty so much for the suggestion. – Mikkel Nicolaysen Aug 05 '22 at 08:08
  • I'm not sure if this is what you are asking for, but I understand that you want to call 'our_font' attributeof fontpageframe class from fontchangerframe class. Have you tried calling `fontpageframe.our_font.config(...)` in `def font_chooser(e)`, instead of `self.our_font.config(..)` ? Let me know if I'm missunderstanding the task (as it is not clear to me whether our_font is the attribute of fontpageframe class only, or whether it is an attribute of both classes) – Vlad Nitu Aug 05 '22 at 09:48

0 Answers0