Tkinter顶层窗口小部件

0 投票
1 回答
2744 浏览
提问于 2025-04-16 06:41
def WhoisWin():

win1 = Toplevel()
win1.title("Whois")
win1.config(bg="black")
win1.geometry("300x300")
win1.resizable(0,0)

text = Text()
text1 = Text()

text1.config(width=15, height=1)
text1.config(bg="black", fg="white")
text1.pack()

def button1():
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect(("com.whois-servers.net", 43))
            s.send(text1.get("1.0", END) + "\r\n")
            response = ''
            while True:
                a = s.recv(4096)
                response += a
                if a == '':
                   break
            s.close()
            text.insert(END, response)

def clear():
        text.delete("1.0", END)  
        text1.delete("1.0", END)       

frame = Frame(win1)
frame.config(bg="black")
frame.pack(pady=10, padx=5)

b = Button(frame, text="Enter", width=10, height=2, command=button1)
b.config(fg="white", bg="black")
b.pack(side=LEFT, padx=5)

c = Button(frame, text="Clear", width=10, height=2, command=clear)
c.config(fg="white", bg="black")
c.pack(side=RIGHT, padx=5)

scrollbar = Scrollbar(win1)
scrollbar.pack(side=RIGHT, fill=Y)
text.config(width=35, height=15, bg="black", fg="white")
text.pack(side=LEFT, fill=Y)
scrollbar.config(command=text.yview)
text.config(yscrollcommand=scrollbar.set)

这只是一个子窗口,当你点击菜单时会弹出来。我没有遇到任何错误,但在这个子窗口上,Text和Tex1看不到。不过当我在主窗口单独运行这段代码时,它是正常工作的。可能是标识符搞错了或者其他什么问题?任何帮助都会很感激,谢谢。

1 个回答

1

你没有给 texttext1 指定一个父级。调用 Text() 的时候,你需要传入一个参数,比如 Text(win1)Text(frame),这样 Tkinter 才知道把这个文本框放在哪个地方。

撰写回答