Tkinter menu icons, toolbars

Source: Internet
Author: User

Pictures used:

                   ImportTkinter as TK fromTkinterImportMessageBox, FileDialog, Simpledialog, Colorchooser fromTkinterImportTTK fromPILImportImage, IMAGETKImport TimeImportThreadingdefShowDialog ():" "various Windows" "    #res = messagebox.askokcancel (title= ' title ', message= ' hint info ... ', Default=messagebox. CANCEL) # Default=messagebox. CANCEL, specifying the default focus location, and another abort/retry/ignore/ok/cancel/yes/no    #res = messagebox.showinfo (title= ' title ', message= ' hint info ... ')    #res = messagebox.showwarning (title= ' title ', message= ' hint info ... ')    #res = messagebox.showerror (title= ' title ', message= ' hint info ... ')    #res = messagebox.askquestion (title= ' title ', message= ' hint info ... ')    #res = Messagebox.askyesno (title= ' title ', message= ' hint info ... ')    #res = messagebox.askyesnocancel (title= ' title ', message= ' hint info ... ')    #res = messagebox.askretrycancel (title= ' title ', message= ' hint info ... ')        #res = filedialog.askdirectory ()    #res = filedialog.askopenfile (filetypes=[(' xml ', ' *.xml ')])    #res = filedialog.askopenfiles ()    #res = filedialog.askopenfilename ()    #res = filedialog.askopenfilenames ()    #res = filedialog.asksaveasfile ()    #res = filedialog.asksaveasfilename ()       #res = Simpledialog.askinteger (title= ' integer ', prompt= ' input an integer ', initialvalue=100)    #res = simpledialog.askfloat (titlee= ' real ', prompt= ' input a real number ', minvalue=0, maxvalue=11)res = simpledialog.askstring (title='string', prompt='Enter a string')        #res = Colorchooser.askcolor ()    Print(RES)classapplication (tk. TK):def __init__(self): Super ().__init__() self.geometry ('677x442')#Window Size        #self.resizable (Width=false, height=false) # Disable resizing of WindowsSelf.createui ()#Build Interface    defCreateui (self): Self.createico () Self.createmenu () Self.createtoolbar () Self.bindall () #Create a menu    defCreateMenu (self):" "supports only two layers of nesting" "Menus= ['file','Edit','Help'] Items= [['New','Open','Save','Save As ...','Close','-','Exit'],                 ['Revoke','-','Cut','Copy','Paste','Delete','Select All',['More ...','Data','Chart','Statistics']],                 ['Index','about the']] Callbacks=[ [ShowDialog, ShowDialog, ShowDialog, ShowDialog, ShowDialog, None, ShowDialog], [ShowDialog, Non                     E, ShowDialog, ShowDialog, ShowDialog, ShowDialog, ShowDialog, [ShowDialog, ShowDialog, ShowDialog]],        [ShowDialog, ShowDialog]] Icos=[ [Self.img1, Self.img2, SELF.IMG3, none, Self.img4, none, none], [Self.img1, None, Self.img2, self.img 3, None, Self.img4, none, [Self.img3, None, Self.img4]], [SELF.IMG1, Self.img2]] menubar =tk. Menu (self) forI,xinchEnumerate (menus): M= Tk. Menu (MenuBar, tearoff=0) forItem, Callback, ICOinchZip (items[i], callbacks[i], icos[i]):ifisinstance (item, list): SM= Tk. Menu (MenuBar, tearoff=0) forSubitem, Subcallback, SubicoinchZip (item[1:], Callback, ICO):ifSubitem = ='-': Sm.add_separator ()Else: Sm.add_command (Label=subitem, Command=subcallback, Image=subico, compound=' Left') m.add_cascade (label=item[0], menu=SM)elifitem = ='-': M.add_separator ()Else: M.add_command (Label=item, Command=callback, Image=ico, compound=' Left') menubar.add_cascade (label=x, menu=m) self.config (menu=menubar)#generate all the required icons    defCreateico (self): SELF.IMG1= Imagetk.photoimage (Image.open ('ico_new_16_16.jpg')) Self.img2= Imagetk.photoimage (Image.open ('ico_open_16_16.jpg')) Self.img3= Imagetk.photoimage (Image.open ('ico_save_16_16.jpg')) Self.img4= Imagetk.photoimage (Image.open ('ico_close_16_16.jpg'))        #self.img5 = imagetk.photoimage (Image.open (' ico_new.jpg '))        #Self.img6 = imagetk.photoimage (Image.open (' ico_open.jpg '))            #Build Tool Bar    defCreatetoolbar (self): Toolframe= Tk. Frame (self, height=20, bg='#F7EED6')#, RELIEF=TK. Raised)frame = tk. Frame (Toolframe, bg='#F7EED6') TTK. Button (frame, Width=20, IMAGE=SELF.IMG1, Command=showdialog). Grid (Row=0, column=0, Padx=1, Pady=1, sticky=tk. E) TTK. Button (frame, Width=20, Image=self.img2, Command=showdialog). Grid (Row=0, Column=1, Padx=1, Pady=1, sticky=tk. E) TTK. Button (frame, Width=20, IMAGE=SELF.IMG3, Command=showdialog). Grid (Row=0, column=2, Padx=1, Pady=1, sticky=tk. E) Frame.pack (Side=tk. left) Toolframe.pack (fill=tk. X)#def createlayout (self):Leftframe = Tk. Frame (self, width=120, bg='#0000FF') TTK. Treeview () Leftframe.pack (side=tk. Left, fill=tk. Y) Mainframe= Tk. Frame (Self, bg='#00FFFF') Tk. Text (mainframe). Pack (Side=tk. Left, fill=tk. X) TTK. Scrollbar (mainframe). Pack (Side=tk. Left,fill=tk. Y) Mainframe.pack (Side=tk. Left, fill=tk. BOTH)#binding shortcut keys    defBindall (self): Self.bind_all ('<Control-n>',LambdaEvent:showdialog ())#The lambda must be here    #package into threads (time-consuming operations)@staticmethoddefThread_it (func, *args): t= Threading. Thread (Target=func, args=args) T.setdaemon (True) T.start () app=application () App.mainloop ( )

Tkinter menu icons, toolbars

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.