【问题标题】:Tkinter Entry appears in top left of screen regardless of grid positioning无论网格定位如何,Tkinter 条目都会出现在屏幕的左上角
【发布时间】:2022-06-19 07:21:35
【问题描述】:

我正在尝试为一个项目编写一个 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()
  • 空行和列的大小为零。你知道吗?
  • @BryanOakley 我不是,我对 tkinter 很陌生。这是什么意思,我该如何解决?非常感谢你

标签: python python-3.x tkinter tkinter-entry


【解决方案1】:

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

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

附言上面的代码仍然很乱。它混合了.pack.place(决定你想要哪个并坚持使用它)并且还有其他问题,但是......与原始代码相比,它应该更容易构建。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 2017-03-26
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多