如何在我的def new_window()函数中放置标有“问题一”的标签。当您运行它时,标签位于底部,我希望将其粘贴在顶部。
from tkinter import *
from tkinter import ttk
#User Interface Code
root = Tk() # Creates the window
root.title("Quiz Game")
def new_window():
newWindow = Toplevel(root)
display = Label(newWindow, width=150, height=40)
message = Label(newWindow, text="Question One", font = ("Arial", "24"))
display.pack()
message.pack()
display2 = Label(root, width=100, height=30, bg='green')
button1 = Button(root, text ="Continue", command=new_window, width=16,
bg="red")
message_label1 = Label(text="A Quiz Game", font = ("Arial", "24"), padx=40,
pady=20)
message_label2 = Label(root, text="Click 'Continue' to begin.",
wraplength=250)
display2.pack()
button1.pack()
message_label1.pack()
message_label2.pack()
root.mainloop() # Runs the main window loop
答案 0 :(得分:2)
您包装的顺序错误。请勿打包显示before
和message
。因此,只需交换订单即可解决问题。
这是代码。将此替换为def new_window():
def new_window():
newWindow = Toplevel()
message = Label(newWindow, text="Question One", font = ("Arial", "24"))
display = Label(newWindow, width=150, height=40)
message.pack()
display.pack()
pack
方法只是盲目地将小部件打包到窗口中。如果有空间,下一个pack
将在其下完成。因此,在包装小部件时要注意顺序:)