Home > Enterprise >  Moving A Tkinter Label To The Center Of My Concurrent Window Size
Moving A Tkinter Label To The Center Of My Concurrent Window Size

Time:10-20

I'm working on a python program that has a "sign in page" and right now only has a button that signs in as I have not added areas for input (password and username) and such, and added a placeholder button that when you press it should take you to some sort of a homepage. Now here's the issue. The button is rendering below the window. As you can see in the screenshot, the button cannot be found as it is located as shown in the second image. (Found this information by making it so I could fullscreen the window and then found it way down at the bottom)

Here's the code if you'd like to see it.

import tkinter as tk
import time
import math


win = tk.Tk()
win.geometry("650x450")
win.resizable(height = False, width = False)
win.configure(background="black")


Signin = tk.Frame(win)
Homepage = tk.Frame(win)


def change_to_Signin():
   win.title("DNO - Sign In")
   Signin.pack(fill="both", expand=1)
   Homepage.pack_forget()

def change_to_Homepage():
   win.title("DNO - Homepage")
   Homepage.pack(fill="both", expand=1)
   Signin.pack_forget()
  
change_to_Signin()


hp_title = tk.Label(Signin, bg="#3e3e3e", text=" Sign In", fg="White", font=('Helvetica bold',25), width=650, height=4, anchor="w").pack(pady=(0, 385))
hp_swapbutton = tk.Button(Signin, text="Sign in", command=change_to_Homepage, font=('Helvetica bold',15)).pack()


#btnP1 = tk.Button(Signin, text = "Sign In", command = change_to_Homepage).pack()
#btnP2 = tk.Button(Homepage, text = "Sign Out", command = change_to_Signin).pack()

win.mainloop()

screenshot with button at the bottom using pack

If instead, you want the button centered in the window, since there are few other widgets you can fairly easily and safely use place with relative coordinates:

hp_swapbutton = tk.Button(Signin, text="Sign in", command=change_to_Homepage, font=('Helvetica bold',15))
hp_swapbutton.place(relx=.5, rely=.5, anchor="center")

screenshot with button in the center using place

  • Related