Aug-23-2020, 09:22 AM
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
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()