Python3.2 按钮BUtton 菜单Mneu 提示ToopTip

来源:互联网 发布:win10平板阅读软件 编辑:程序博客网 时间:2024/04/28 19:21

这些例子都是网上找的

----------------按钮BUtton ---------------------------------

from tkinter import *

class GUI:
    def __init__(self):
        self.root = Tk()
        self.root.title('Button Styles')
        for bdw in range(5):
            setattr(self, 'of%d' % bdw, Frame(self.root, borderwidth=0))
            Label(getattr(self, 'of%d' % bdw),
                  text='borderwidth = %d  ' % bdw).pack(side=LEFT)
            for relief in [RAISED, SUNKEN, FLAT, RIDGE, GROOVE, SOLID]:
                print(getattr(self, 'of%d' % bdw))
                Button(getattr(self, 'of%d' % bdw), text=relief, borderwidth=bdw,
                       relief=relief, width=10,
                       command=lambda s=self, r=relief, b=bdw: s.prt(r,b))/
                          .pack(side=LEFT, padx=7-bdw, pady=7-bdw)
            getattr(self, 'of%d' % bdw).pack()

    def prt(self, relief, border):
        print ('%s:%d' % (relief, border))

myGUI = GUI()
myGUI.root.mainloop()


------------------------ 菜单Mneu ------------------------------------

from tkinter import *

def new_file():
    print("Open new file")

def open_file():
    print("Open existing file")

def stub_action():
    print("Menu select")

def makeCommandMenu():
    CmdBtn = Menubutton(mBar, text='Button Commands', underline=0)
    CmdBtn.pack(side=LEFT, padx="2m")
    CmdBtn.menu = Menu(CmdBtn)

    CmdBtn.menu.add_command(label="Undo")
    CmdBtn.menu.entryconfig(0, state=DISABLED)

    CmdBtn.menu.add_command(label='New...', underline=0, command=new_file)
    CmdBtn.menu.add_command(label='Open...', underline=0, command=open_file)
    CmdBtn.menu.add_command(label='Wild Font', underline=0,
    font=('Tempus Sans ITC', 14), command=stub_action)
    #CmdBtn.menu.add_command(bitmap="@bitmaps/RotateLeft")
    CmdBtn.menu.add('separator')
    CmdBtn.menu.add_command(label='Quit', underline=0,
    background='white', activebackground='green',
    command=CmdBtn.quit)

    CmdBtn['menu'] = CmdBtn.menu
    return CmdBtn

def makeCascadeMenu():
    CasBtn = Menubutton(mBar, text='Cascading Menus', underline=0)
    CasBtn.pack(side=LEFT, padx="2m")
    CasBtn.menu = Menu(CasBtn)
    CasBtn.menu.choices = Menu(CasBtn.menu)
    CasBtn.menu.choices.wierdones = Menu(CasBtn.menu.choices)

    CasBtn.menu.choices.wierdones.add_command(label='A')
    CasBtn.menu.choices.wierdones.add_command(label='B')
    CasBtn.menu.choices.wierdones.add_command(label='C')
    CasBtn.menu.choices.wierdones.add_command(label='D')   

    CasBtn.menu.choices.add_command(label='A')
    CasBtn.menu.choices.add_command(label='B')
    CasBtn.menu.choices.add_command(label='C')
    CasBtn.menu.choices.add_command(label='D')
    CasBtn.menu.choices.add_command(label='E')
    CasBtn.menu.choices.add_command(label='F')
    CasBtn.menu.choices.add_cascade(label='G',
  menu=CasBtn.menu.choices.wierdones)

    CasBtn.menu.add_cascade(label='Scipts', menu=CasBtn.menu.choices)
    CasBtn['menu'] = CasBtn.menu
    return CasBtn

def makeCheckbuttonMenu():
    ChkBtn = Menubutton(mBar, text='Checkbutton Menus', underline=0)
    ChkBtn.pack(side=LEFT, padx='2m')
    ChkBtn.menu = Menu(ChkBtn)

    ChkBtn.menu.add_checkbutton(label='A')
    ChkBtn.menu.add_checkbutton(label='B')
    ChkBtn.menu.add_checkbutton(label="C")
    ChkBtn.menu.add_checkbutton(label='D')
    ChkBtn.menu.add_checkbutton(label='E')   
    ChkBtn.menu.invoke(ChkBtn.menu.index('C'))

    ChkBtn['menu'] = ChkBtn.menu
    return ChkBtn

def makeRadiobuttonMenu():
    RadBtn = Menubutton(mBar, text='Radiobutton Menus', underline=0)
    RadBtn.pack(side=LEFT, padx='2m')
    RadBtn.menu = Menu(RadBtn)

    RadBtn.menu.add_radiobutton(label='A')
    RadBtn.menu.add_radiobutton(label='B')
    RadBtn.menu.add_radiobutton(label='C')
    RadBtn.menu.add_radiobutton(label='D')
    RadBtn.menu.add_radiobutton(label='E')
    RadBtn.menu.add_radiobutton(label='F')
    RadBtn.menu.add_radiobutton(label='G')
    RadBtn.menu.add_radiobutton(label='H')
    RadBtn.menu.add_radiobutton(label='I')

    RadBtn['menu'] = RadBtn.menu
    return RadBtn


def makeDisabledMenu():
    Dummy_button = Menubutton(mBar, text='Disabled Menu', underline=0)
    Dummy_button.pack(side=LEFT, padx='2m')
    Dummy_button["state"] = DISABLED
    return Dummy_button

root = Tk()
mBar = Frame(root, relief=RAISED, borderwidth=2)
mBar.pack(fill=X)

CmdBtn = makeCommandMenu()
CasBtn = makeCascadeMenu()
ChkBtn = makeCheckbuttonMenu()
RadBtn = makeRadiobuttonMenu()
NoMenu = makeDisabledMenu()

mBar.tk_menuBar(CmdBtn, CasBtn, ChkBtn, RadBtn, NoMenu)

root.title('Menus')
root.mainloop()


---------------------提示ToopTip--------------------------------------

from tkinter import *
from time import time, localtime, strftime

class ToolTip( Toplevel ):
    """
    Provides a ToolTip widget for Tkinter.
    To apply a ToolTip to any Tkinter widget, simply pass the widget to the
    ToolTip constructor
    """
    def __init__( self, wdgt, msg=None, msgFunc=None, delay=1, follow=True ):
        """
        Initialize the ToolTip
       
        Arguments:
          wdgt: The widget this ToolTip is assigned to
          msg:  A static string message assigned to the ToolTip
          msgFunc: A function that retrieves a string to use as the ToolTip text
          delay:   The delay in seconds before the ToolTip appears(may be float)
          follow:  If True, the ToolTip follows motion, otherwise hides
        """
        self.wdgt = wdgt
        self.parent = self.wdgt.master                                          # The parent of the ToolTip is the parent of the ToolTips widget
        Toplevel.__init__( self, self.parent, bg='black', padx=1, pady=1 )      # Initalise the Toplevel
        self.withdraw()                                                         # Hide initially
        self.overrideredirect( True )                                           # The ToolTip Toplevel should have no frame or title bar
       
        self.msgVar = StringVar()                                               # The msgVar will contain the text displayed by the ToolTip       
        if msg == None:                                                        
            self.msgVar.set( 'No message provided' )
        else:
            self.msgVar.set( msg )
        self.msgFunc = msgFunc
        self.delay = delay
        self.follow = follow
        self.visible = 0
        self.lastMotion = 0
        Message( self, textvariable=self.msgVar, bg='#FFFFDD',
                 aspect=1000 ).grid()                                           # The test of the ToolTip is displayed in a Message widget
        self.wdgt.bind( '<Enter>', self.spawn, '+' )                            # Add bindings to the widget.  This will NOT override bindings that the widget already has
        self.wdgt.bind( '<Leave>', self.hide, '+' )
        self.wdgt.bind( '<Motion>', self.move, '+' )
       
    def spawn( self, event=None ):
        """
        Spawn the ToolTip.  This simply makes the ToolTip eligible for display.
        Usually this is caused by entering the widget
       
        Arguments:
          event: The event that called this funciton
        """
        self.visible = 1
        self.after( int( self.delay * 1000 ), self.show )                       # The after function takes a time argument in miliseconds
       
    def show( self ):
        """
        Displays the ToolTip if the time delay has been long enough
        """
        if self.visible == 1 and time() - self.lastMotion > self.delay:
            self.visible = 2
        if self.visible == 2:
            self.deiconify()
           
    def move( self, event ):
        """
        Processes motion within the widget.
       
        Arguments:
          event: The event that called this function
        """
        self.lastMotion = time()
        if self.follow == False:                                                # If the follow flag is not set, motion within the widget will make the ToolTip dissapear
            self.withdraw()
            self.visible = 1
        self.geometry( '+%i+%i' % ( event.x_root+10, event.y_root+10 ) )        # Offset the ToolTip 10x10 pixes southwest of the pointer
        try:
            self.msgVar.set( self.msgFunc() )                                   # Try to call the message function.  Will not change the message if the message function is None or the message function fails
        except:
            pass
        self.after( int( self.delay * 1000 ), self.show )
           
    def hide( self, event=None ):
        """
        Hides the ToolTip.  Usually this is caused by leaving the widget
       
        Arguments:
          event: The event that called this function
        """
        self.visible = 0
        self.withdraw()

def xrange2d( n,m ):
    """
    Returns a generator of values in a 2d range
   
    Arguments:
      n: The number of rows in the 2d range
      m: The number of columns in the 2d range
    Returns:
      A generator of values in a 2d range
    """
    return ( (i,j) for i in xrange(n) for j in xrange(m) )

def range2d( n,m ):
    """
    Returns a list of values in a 2d range
   
    Arguments:
      n: The number of rows in the 2d range
      m: The number of columns in the 2d range
    Returns:
      A list of values in a 2d range
    """
    return [(i,j) for i in range(n) for j in range(m) ]

def print_time():
    """
    Prints the current time in the following format:
    HH:MM:SS.00
    """
    t = time()
    timeString = 'time='
    timeString += strftime( '%H:%M:', localtime(t) )
    timeString += '%.2f' % ( t%60, )
    return timeString
   
def main():
    root = Tk()
    btnList = []
    for (i,j) in range2d( 6, 4 ):
        text = 'delay=%i/n' % i
        delay = i
        if j >= 2:
            follow=True
            text += '+follow/n'
        else:
            follow = False
            text += '-follow/n'
        if j % 2 == 0:
            msg = None
            msgFunc = print_time
            text += 'Message Function'
        else:
            msg = 'Button at %s' % str( (i,j) )
            msgFunc = None
            text += 'Static Message'
        btnList.append( Button( root, text=text ) )
        ToolTip( btnList[-1], msg=msg, msgFunc=msgFunc, follow=follow, delay=delay)
        btnList[-1].grid( row=i, column=j, sticky=N+S+E+W )
    root.mainloop()
   
if __name__ == '__main__':
    main()


-------------------------------------------------------------

原创粉丝点击