我正在嘗試為一個專案撰寫一個 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()
uj5u.com熱心網友回復:
你的代碼有很多問題,我決定簡化它。現在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()
PS上面的代碼還是亂七八糟的。它混合了.pack和.place(決定你想要哪個并堅持使用它)并且還有其他問題,但是......與原始代碼相比,它應該更容易構建。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/492988.html
標籤:Python python-3.x tkinter tkinter-entry
上一篇:在另一個TabControl中添加新的TabControls
下一篇:調整影像大小后質量下降
