You need to enable JavaScript to run this app. 使用Zelle graphics.py库时如何让图形窗口始终保持置顶?
优惠活动
大模型
产品
解决方案
定价
更多

使用Zelle graphics.py库时如何让图形窗口始终保持置顶?

解决Zelle graphics.py库生成窗口无法保持置顶的问题

实现原理

Zelle的graphics.py底层基于Tkinter封装,你创建的GraphWin实例本身就是Tkinter组件的封装对象,直接获取其对应的顶层窗口,设置-topmost属性即可实现窗口强制置顶,逻辑和turtle库的实现方案完全一致,仅获取顶层窗口的方式略有区别。

完整修改代码

from graphics import *

win = GraphWin("MyWindow", 200, 200)
# 新增以下两行代码实现窗口置顶
root_window = win.winfo_toplevel()
root_window.attributes('-topmost', True)
# 也可以沿用你找到的turtle风格调用写法,效果一致:
# root_window.call('wm','attributes','.','-topmost','1')

def cir(radius):
    cir = Circle(Point(30,30), radius)
    cir.setFill("red")
    cir.draw(win)
    return

radius=int(input("what size of circle?"))
cir(radius)
win.getMouse()
win.close()

补充说明

  • 如需后续取消置顶状态,只需调用root_window.attributes('-topmost', False)即可
  • 该属性在Windows、macOS、主流Linux桌面环境下均生效

内容的提问来源于stack exchange,提问作者netrate

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.19 03:36:04