Who_Is()函数输出未在tkinter窗口显示仅出现在VS Code终端如何解决
问题原因
你当前代码中使用的print()方法仅会将内容输出到程序运行的控制台终端,没有关联Tkinter窗口内的任何展示组件,因此查询结果不会显示在GUI界面中。
解决方法
你需要在窗口中新增文本展示组件(推荐用Text组件,支持多行内容展示和滚动),再将查询结果写入该组件即可,修改后的完整可运行代码如下:
from tkinter import * from tkinter.ttk import * import whois # pip3 install python-whois root = Tk() root.geometry('1000x800') root.title('Who Is') menubar = Menu(root) lookup = "example.com" # 新增带滚动条的结果展示区域 frame = Frame(root) frame.pack(fill=BOTH, expand=True, padx=10, pady=10) scrollbar = Scrollbar(frame) scrollbar.pack(side=RIGHT, fill=Y) # Text组件默认设为只读,插入内容时临时开放编辑权限 result_text = Text(frame, yscrollcommand=scrollbar.set, state=DISABLED, font=("微软雅黑", 12)) result_text.pack(fill=BOTH, expand=True) scrollbar.config(command=result_text.yview) def Who_Is(): res = whois.whois(lookup) # 组装查询结果内容 show_content = f"[+] Domain: {res.domain}\n[+] Organization: {res.get('org')}\n" # 写入结果到GUI组件 result_text.config(state=NORMAL) # 不需要清空旧结果可以删除下一行 result_text.delete(1.0, END) result_text.insert(END, show_content) result_text.config(state=DISABLED) # 原有菜单逻辑保持不变 recon = Menu(menubar, tearoff=0) menubar.add_cascade(label='Recon', menu=recon) recon.add_command(label='Who Is Lookup', command=lambda: Who_Is()) recon.add_separator() recon.add_command(label='Exit', command=root.destroy) root.config(menu=menubar) mainloop()
改动说明
- 新增了带滚动条的
Text组件作为结果展示区域,默认设置为只读状态,避免用户误改查询结果 - 把原
Who_Is()函数中的print逻辑替换为向Text组件插入内容的逻辑,可根据需求选择是否插入前清空旧结果
内容的提问来源于stack exchange,提问作者Nitheesh Paul
相关产品推荐
相关产品推荐

