【问题标题】:Non-standard windows with Tkinter?Tkinter 的非标准窗口?
【发布时间】:2012-09-26 15:16:59
【问题描述】:

有没有办法用 Tkinter 创建非标准窗口?我想在屏幕上显示一些浮动图像,里面有 Tkinter 小部件。想想 Mac 的 Growl、iPad 上的 Siri、Mac OS 音量/亮度变化边框等。如果这不可能,有没有办法摆脱带有标题的窗口顶部栏并关闭/最小化/resize 按钮,并且需要脚本完成(或修饰符-Q 击键)才能关闭?

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    wm_overrideredirect 将删除标准窗口边框。你仍然会被一个矩形窗口困住。您可以使用wm_attributes(查找alpha 属性)调整窗口的透明度,但这仅适用于Windows 和Mac。

    已经尝试过使用 tcl/tk 来塑造窗口,尽管它需要编译一些代码,但您也许可以使用 Tkinter。请参阅 tcl'ers wiki 上的 Managed and shaped toplevel

    【讨论】:

      【解决方案2】:

      是的,你可以!你可以使用

      app.overrideredirect(True)

      其中 app = Tk()

      从这里你被困在一个不是顶层的窗口中;它也没有任务栏位置; 为了解决这个问题,我们可以使用: app.attributes('-topmost', 1) 但即使现在你也只有一个矩形;您需要创建一个自定义标题栏;这只是一个带有标题、退出、最小化按钮等的框架。

      要从我们使用的标题栏移动窗口:

          def get_pos(event):
          xwin = app.winfo_x()
          ywin = app.winfo_y()
          startx = event.x_root
          starty = event.y_root
      
          ywin = ywin - starty
          xwin = xwin - startx
      
      
          def move_window(event):
          app.geometry("400x400" + '+{0}+{1}'.format(event.x_root + xwin, event.y_root + ywin))
          startx = event.x_root
          starty = event.y_root
      
      
          app.TopFrame.bind('<B1-Motion>', move_window)
      app.TopFrame.bind('<Button-1>', get_pos)
      

      TopFrame 是标题栏。这种方法看起来过于复杂,但是它允许栏直接在鼠标上移动,而不会移动到角落。

      回答您的后一个问题: 制作一个漂浮的物体是很有可能的;使用:

      app.overrideredirect(True)
      app.image = tk.PhotoImage(file='image.GIF')
      image = Label(root, image=app.image, bg='white')
      app.attributes("-transparentcolor", "white")
      

      【讨论】:

        猜你喜欢
        • 2013-09-17
        • 2013-10-21
        • 2018-02-18
        • 2010-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-28
        • 1970-01-01
        相关资源
        最近更新 更多