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!
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!
It is probably because there is only one widget on this window,
you may want to use padx
and pady
in your grid function.
Tk()
multiple times is going to cause you various problems. UseToplevel()
instead to create additional windows. – jasonharper Commented Jan 2 at 14:53Toplevel()
, 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! – Alessio Spinellino Commented Jan 2 at 17:04