gpt4 book ai didi

python - Tkinter GUI 顶层

转载 作者:行者123 更新时间:2023-11-28 21:59:48 26 4
gpt4 key购买 nike

from tkinter import*
import tkinter as tk

def topLevel():
top=Toplevel()
top.title("Listbox test")
notiLabel = Label(text ="----test----", font=('Times', 20))
notiLabel.pack()
notiLabel.grid(row=0,column=0, sticky=W)

noti = Label(text ="----test----", font=('Times', 18))
noti.pack()
noti.grid(row=1,column=1, sticky=W)

f = Label(text ="------test-----")
f.pack()
f.grid(row=3,column=0, sticky=W)
fa = Label()
fa.pack()
fa.grid(row=3,column=1, sticky=W)


root=tk.Tk()
root.title("Listbox test")

s = tk.Label(text =">>>test<<<", font=(('Times'),20))
s.pack()
s.grid(row=2,column=0)


N = tk.Label(text =">>>test<<<")
N.pack()
N.grid(row=3,column=0)


LB = tk.Listbox(width=50, selectmode =SINGLE)
LB.pack()
LB.grid(row=4, column=0)


TI = tk.Button(text="b1", width =50, command=topLevel)
TI.pack()
TI.grid(row=5, column=0)

root.mainloop()

程序运行时,点击命令按钮b1后,顶层窗口的信息和标签仍然打印在下层窗口,如何解决?

最佳答案

您必须指定在哪个框架(Toplevel())上添加新的小部件:

from Tkinter import *

def topLevel():
top=Toplevel()
top.title("Listbox test")
notiLabel = Label(top, text ="----test----", font=('Times', 20))
notiLabel.grid(row=0,column=0, sticky=W)

noti = Label(top, text ="----test----", font=('Times', 18))
noti.grid(row=1,column=1, sticky=W)

f = Label(top, text ="------test-----") # note the 'top' parameter
# 'top' was your Toplevel widget
f.grid(row=3,column=0, sticky=W)
fa = Label(top)
fa.grid(row=3,column=1, sticky=W)


root=Tk()
root.title("Listbox test")

s = Label(text =">>>test<<<", font=(('Times'),20))
s.grid(row=2,column=0)


N = Label(text =">>>test<<<")
N.grid(row=3,column=0)


LB = Listbox(width=50, selectmode =SINGLE)
LB.grid(row=4, column=0)


TI = Button(text="b1", width =50, command=topLevel)
TI.grid(row=5, column=0)

root.mainloop()

我还摆脱了 .pack().grid() 的用法,只坚持使用 .grid().

关于python - Tkinter GUI 顶层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15992756/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com