无论网格定位如何
我正在尝试为一个项目编码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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码有很多问题,我决定简化它。现在,
条目
出现在中心中的解决问题: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: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.