简体   繁体   English

Python Tkinter:如何将自定义边框添加到弹出窗口

[英]Python Tkinter: How to add custom border to Pop-up window

The following is a sample of my code. 以下是我的代码示例。 I do not understand why I cannot create a thin black border surrounding my pop-up window. 我不明白为什么无法在弹出窗口周围创建黑色细边框。

Note that I have purposely remove the default border using self.top.overrideredirect(True) . 请注意,我故意使用self.top.overrideredirect(True)删除了默认边框。 However, I want to re-create a thin layer. 但是,我想重新创建一个薄层。

Please try the following (Python 2.7): 请尝试以下操作(Python 2.7):

from Tkinter import Tk, Frame, Toplevel, Label

class srcDestDescription(Frame):
   def __init__(self, parent):
      Frame.__init__(self, parent)
      self.top = Toplevel(parent)
      self.parent = parent
      self.top.overrideredirect(True)
      self.top.geometry('+%d+%d' % (350, 100))
      self.top.withdraw()
   def display(self):
      self.top.deiconify()
      topLabel = Label(self.top, text = "Hello World")
      topLabel.grid(column = 0, columnspan = 2, row = 0, sticky = 'NSWE', padx = 10, pady = (10, 5))
      topLabel.config(font = ('times', 14))

class Application(Frame):

   def __init__(self, parent):
      Frame.__init__(self, parent)
      self.parent = parent
      self.parent.geometry('%dx%d+%d+%d' % (300, 300, 0, 0))
      self.parent.resizable(0, 0)

      self.pack(expand = True)
      self.srcDestDescription = srcDestDescription(self)
      self.srcDestDescription.display()

def main():
   root = Tk()
   Application(root)
   root.mainloop()

if __name__ == '__main__':
   main()

The following works for me 以下对我有用

from Tkinter import Tk, Frame, Toplevel, Label

class MyTop(Toplevel):
   def __init__(self, parent):
      Toplevel.__init__(self, parent)
      self.overrideredirect(True)
      self.withdraw()
      self.geometry('+%d+%d' % (350, 100))
      topLabel = Label(self, text="Hello World",
                       borderwidth=1, relief='solid')
      topLabel.pack(padx=10, pady=(10, 5))
      topLabel.config(font = ('times', 14))

class Application(Frame):

   def __init__(self, parent):
      Frame.__init__(self, parent)
      self.parent = parent
      self.parent.geometry('%dx%d+%d+%d' % (300, 300, 0, 0))
      self.parent.resizable(0, 0)

      self.pack(expand = True)
      self.mytop = MyTop(self.parent)
      self.mytop.deiconify()

def main():
   root = Tk()
   Application(root)
   root.mainloop()

if __name__ == '__main__':
   main()

Code for a tool tip popup, based on Toplevel but with a class that is not a Toplevel subclass, can be found in Pythonxy/lib/idlelib/ToolTip.py. 可以在Pythonxy / lib / idlelib / ToolTip.py中找到基于Toplevel但具有不是Toplevel子类的类的工具提示弹出窗口的代码。 (Copy this is you want to use it.) Idle's CalltipWindow popup are similar but a bit more complicated. (复制此文件是您要使用的。)Idle的CalltipWindow弹出窗口类似,但更为复杂。

Another way to get a thin-border popup is to use a Menu instance and tk_popup(self, x, y, entry='') (docstring "Post the menu at position X,Y with entry ENTRY."). 获取薄边框弹出窗口的另一种方法是使用Menu实例和tk_popup(self, x, y, entry='') (文档字符串“将菜单条目ENTRY放在X,Y位置)”。 The typical use is for a right-click context menu, but you invent another use. 通常用于右键单击上下文菜单,但是您发明了另一种用法。 The Idle example is Pythonxy/Lib/idlelib/EditorWindow.py, EditorWindow.right_menu_event. 空闲示例是Pythonxy / Lib / idlelib / EditorWindow.py,EditorWindow.right_menu_event。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM