python - Layout issue with 'label.grid' - Stack Overflow

admin2025-05-01  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!

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!

Share Improve this question edited Jan 5 at 12:40 ZCGCoder 5382 gold badges10 silver badges30 bronze badges asked Jan 2 at 14:39 Alessio SpinellinoAlessio Spinellino 971 silver badge6 bronze badges 3
  • Note that calling Tk() multiple times is going to cause you various problems. Use Toplevel() instead to create additional windows. – jasonharper Commented Jan 2 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. – acw1668 Commented Jan 2 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! – Alessio Spinellino Commented Jan 2 at 17:04
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://anycun.com/QandA/1746114380a91868.html