Home > Blockchain >  Tkinter Entry appears in top left of screen regardless of grid positioning
Tkinter Entry appears in top left of screen regardless of grid positioning

Time:06-19

I'm trying to code a GUI for a project and I'm trying to get an entry text box in the center. I've tried to change the row and column values but they just keep the box in the top left. Any ideas on how I can center the text box? (Problem is with takeEntry function)

import tkinter as tk
from tkinter import *

# Startup
root = tk.Tk()
root.geometry("600x400")
root.title('SpellIt')
canvas = tk.Canvas(root,width=600, height=400)
canvas.grid(columnspan=3, rowspan=10)

def startPage(root):
    start_page = tk.Frame(root)
    start_page.grid(columnspan=3, rowspan=10)
    instructions = tk.Label(root, text="Spell It!", font=("Impact", 44))
    instructions.grid(columnspan=3, column=0, row=0)
    start_text = tk.StringVar()
    start_button = tk.Button(root, textvariable=start_text, command=lambda: changepage(), font=("Impact", 30))
    start_text.set("Start")
    start_button.grid(columnspan=3, column=0, row=5)


def gamePage(root):
    game_page = tk.Frame(root, width=600, height=400)
    game_page.grid(columnspan=3, rowspan=10)
    tk.Label(game_page, text = "This is the game page").grid(row = 0)

def changepage():
    global pagenum, root
    for widget in root.winfo_children():
        widget.destroy()
    if pagenum == 1:
        gamePage(root)
        takeEntry()
        clock(60)
        pagenum = 2

# Timer
def clock(count):
    global root
    timer_label = tk.Label(root, text="", font=("Impact", 20))
    timer_label.place(x=500, y=15)
    # change text in label
    timer_label['text'] = count

    if count > 0:
        # call countdown again 1s
        root.after(1000, clock, count - 1)

def takeEntry():
    entry1 = Entry(root, width = 30, font=("Impact", 10)).grid(row=1, column=1) #problem is here

pagenum = 1
startPage(root)

root.mainloop()

CodePudding user response:

Your code has so many issues that I decided to simplify it. Now the Entry appears in the center what solves your problem:

from tkinter import Tk, Entry, Label, Button
def changepage():
    global pagenum, timer_label, entryWidget
    for widget in root.winfo_children():
        widget.destroy()
    if pagenum == 1:
        pagenum = 2
        Label(root, text = "This is the game page").pack()
        entry_widget = Entry(root, width = 30, font=("Impact", 10))
        entry_widget.pack()
        timer_label = Label(root, text="", font=("Impact", 20))
        timer_label.place(x=500, y=15)
        clock(60)
# Timer
def clock(count):
    # change text in label
    timer_label['text'] = count
    if count > 0:
        # call countdown again 1s
        root.after(1000, clock, count - 1)
root = Tk()
root.geometry("600x400")
root.title('SpellIt')
Label( root, text="Spell It!", font=("Impact", 44)).pack()
Button(root, text="Start", command=changepage, font=("Impact", 30)).pack()
pagenum = 1
root.mainloop()

P.S. The code above is still messy. It mixes .pack and .place (decide which you want and stick with it) and has also other issues, but ... it should be easier for you to build upon it compared to your original code.

  • Related