0

I am writing a Python text editor with TkInter, and I'm writing the "Preferences" window to edit the font, the font dimension etc. It is in the __openPreferences() method in the LampText class. I'm putting a label in it with the "Choose default font:" text to introduce a dropdown list to select one of the avaiable fonts. The problem is, when I write lblFont.grid(row=1, column=1) it doesn't work -- it remains in the default position. Even if I use bigger numbers, it still doesn't work. I have no idea about the problem.

Here's the code:

import tkinter
class Menu:
 def __init__(self, parent, font):
 menu = tkinter.Menu(parent.mainWin, font=font)
 parent.mainWin.config(menu=menu)
 dpdFile = tkinter.Menu(menu, font=font, tearoff=0)
 dpdFile.add_command(label="New")
 dpdFile.add_command(label="Open")
 dpdFile.add_command(label="Save")
 dpdFile.add_command(label="Save As")
 dpdFile.add_command(label="New")
 dpdFile.add_separator()
 dpdFile.add_command(label="Exit")
 
 dpdAbout = tkinter.Menu(menu, font=font, tearoff=0)
 dpdAbout.add_command(label="About")
 dpdAbout.add_command(label="Release Notes")
 dpdPref = tkinter.Menu(menu, font=font, tearoff=0)
 dpdPref.add_command(label="Change Preferences", command=parent._LampText__openPreferences)
 menu.add_cascade(label="File", menu=dpdFile)
 menu.add_cascade(label="About", menu=dpdAbout)
 menu.add_cascade(label="Preferences", menu=dpdPref)
class LampText:
 def __init__(self, mainWin, txFont, mnFont):
 self.final = " - LampText 1.0"
 mainWin.title("Untitled" + self.final)
 mainWin.geometry("1200x600")
 self.mainWin = mainWin
 
 self.txArea = tkinter.Text(mainWin, font=txFont)
 self.scrBar = tkinter.Scrollbar(mainWin, command=self.txArea.yview)
 self.txArea.configure(yscrollcommand=self.scrBar.set)
 self.txArea.pack(side=tkinter.LEFT, fill=tkinter.BOTH, expand=True)
 self.scrBar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
 self.topBar = Menu(self, mnFont)
 
 def __openPreferences(self):
 prefWin = tkinter.Tk()
 prefWin.title("Preferences - Notebook 1.0")
 prefWin.geometry("600x300")
 lblFont = tkinter.Label(prefWin, text="Choose Default Font:")
 lblFont.grid(row=1, column=1)
 prefWin.mainloop()
if __name__=='__main__':
 win = tkinter.Tk()
 txFnt = "Tebuchet MS"
 txDim = "10"
 txFont = (txFnt, txDim)
 mnFnt = "Tebuchet MS"
 mnDim = "8"
 mnFont = (mnFnt, mnDim)
 LampTextWin = LampText(win, txFont, mnFont)
 
 win.mainloop()

Thank you really much for your help!

ZCGCoder
5422 gold badges11 silver badges31 bronze badges
asked Jan 2, 2025 at 14:39
3
  • Note that calling Tk() multiple times is going to cause you various problems. Use Toplevel() instead to create additional windows. Commented Jan 2, 2025 at 14:53
  • 1
    Rows and columns without widget will be of size 0. So if the label is the only one widget in the window, then no matter which row and column are used, it is always put in the top-left corner. Commented Jan 2, 2025 at 14:54
  • Hello! I tried Toplevel(), and it works. Can you explain me better how I would solve my issue? That's because I don't figure out exactly what's a possible solution to this problem you explained to me. Thank you very much for the help! Commented Jan 2, 2025 at 17:04

1 Answer 1

0

It is probably because there is only one widget on this window, you may want to use padx and pady in your grid function.

Kovy Jacob
1,19111 silver badges26 bronze badges
answered Jan 2, 2025 at 14:50
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.