Python Forum
Learnig the use of grid
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Learnig the use of grid
#1
Hi,

I'm more comfortable using the grid to align widgets, but I just cannot get it right. The following code is a simple login page based on a mainframe and the system won't allow the use of grid.

What's the workaround?

[Using PyCharm on Win10]

TIA

import tkinter as tk             # python 3
from tkinter import font  as tkfont # python 3

#variables
GEO = "600x600+400+200"
APPNAME = 'Demo app'
BGCOLOR = "yellow"
FGCOLOR = "black"
FONT = 'Times 32' #'Comic Sans MS' 'Helvetica'
FONTSIZE = 12

class MainApp(tk.Tk):
  def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)

    self.title_font = tkfont.Font(family=FONT, size=18,
                                  weight="bold",
                                  slant="italic")

    container = tk.Frame(self)
    container.pack(side="top", fill="both", expand=True)
    container.grid_rowconfigure(0, weight=1)
    container.grid_columnconfigure(0, weight=1)

    self.geometry(GEO)
    self.frames = {}

    for F in (Login, Conf, Data):
      page_name = F.__name__
      frame = F(parent=container, controller=self)
      self.frames[page_name] = frame

      frame.grid(row=0, column=0, sticky="nsew")

    self.show_frame("Login")

  def show_frame(self, page_name):
    '''Show a frame for the given page name'''
    frame = self.frames[page_name]
    frame.tkraise()


class Login(tk.Frame):

  def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller
    self.config(bg=BGCOLOR)

    welcomeText="Welcome! Please login."
    lblWelcome = tk.Label(self, text=welcomeText,
                       bg=BGCOLOR,
                       fg=FGCOLOR,
                       font=(FONT, FONTSIZE))#.pack()

    lblUsername = tk.Label(self, text="Username:",
                           bg=BGCOLOR,
                           fg=FGCOLOR,
                           font=(FONT, FONTSIZE)) #.pack()
    #lblUsername.place(x=40, y=70)  #<-- works
    lblUsername.grid(controller.column=1, controller.row=3) # <-- fails

    btnExit = tk.Button(self, text="Exit",
                       command=quit)
    btnExit.pack(side='bottom',  pady=15)


class Conf(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.config(bg=BGCOLOR)


class Data(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.config(bg=BGCOLOR)

if __name__ == "__main__":
  app = MainApp()
  app.mainloop()
Reply
#2
lblUsername.grid(controller.column=1, controller.row=3)
should be
lblUsername.grid(column=1, row=3)
lblUsername.grid() is expecting named arguments like "column" and "row". Your code is trying to do assignment. What error do you get? You should include that in your post.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020