无论网格定位如何

发布于 2025-02-08 13:04:12 字数 1636 浏览 1 评论 0原文

我正在尝试为一个项目编码GUI,并且正在尝试将入口文本框放在中心。我尝试更改行和列值,但它们只是将框放在左上方。关于我如何将文本框居中的任何想法? (问题是Takeentry功能)

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()

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2025-02-15 13:04:12

您的代码有很多问题,我决定简化它。现在,条目出现在中心中的解决问题:

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()

ps 上面的代码仍然凌乱。它混合.pack。放置(确定您想要哪个并坚持下去),并且还有其他问题,但是...与原始代码相比,您应该更容易建立它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文