Tkinter rapide
Transcription
Tkinter rapide
Bibliothèque graphique Tkinter Python et Interface graphique ● ● ● ● ● ● Bibliothèque graphique la plus utilisée Adaptation de la bibliothèque Tk développée à l'origine pour le langage Tcl Alternatives : wxPython, pyQT, pyGTK, ... Bibliothèques de widgets java, MFC de windows Versions de Tkinter pour windows, linux et mac Tkinter : simplicité adéquate au prototypage rapide Premier pas ● Importation de la bibliothèque complète : from Tkinter import * ● Exemple : fen=Tk() text=Label(fen,text='Bonjour',fg='red') text.pack() bou=Button(fen,text='Quitter',command=fen.destroy) bou.pack() fen.mainloop() Positionnement des widgets ● La méthode pack() – – – – ● Attribut : side Valeurs : LEFT, RIGHT, TOP, BOTTOM Intervalle autour : padx, pady Combinable avec Frame La méthode grid() – – – – Attributs : row et column Valeurs : entier positif ou nul Alignement : sticky=N, S, W ou E Intervalle autour : padx, pady Affichage d'images ● La méthode place() ● Format gif de préférence (ou bibliothèque PIL) fen=Tk() can=Canvas(fen) photo=PhotoImage(file='monImage.gif') item=can.create_image(80,80,image=photo) fen.mainloop() Gestionnaire d'évènements from Tkinter import * from math import * def evaluer(event): chaine.configure(text='Résultat = '+str(eval(entree.get()))) fen=Tk() entree=Entry(fen) entree.bind('<Return>',evaluer) chaine=Label(fen) entree.pack() chaine.pack() fen.mainloop() Gestionnaire d'évènement From Tkinter import * def pointeur(event): chaine.configure(text='clic détecté en X='+str(event.x) +' y='+str(event.y)) fen=Tk() cadre=Frame(fen,width=200,height=150,bg='light yellow') cadre.bind('<Button-1>',pointeur) cadre.pack() chaine=Label(fen) chaine.pack() fen.mainloop() Attributs standards (1) ● Dimensions – – ● ● entier : pixel (par défaut) chaîne contenant un entier + c,i,m ou p Système de coordonnées : (0,0) en haut à gauche Couleurs : – – Prédéfinis ('red', 'green', light yellow'...) #rgb, #rrggbb, #rrrgggbbb Attributs standards (2) ● font – – – – ● Tuple : ('Times','24','bold italic ...') Bibliothèque tkFont : font=tkFont.Font(options...) X font names sous X windows tkFont.families() : liste des familles de la plateforme anchor : NW N NE W CENTER E SW N SE Attributs standards (3) ● relief : flat, raised, sunken, groove, ridge ● bitmaps : – – Prédéfinies : 'error', 'info','question','warning',... '@nomBitmab.xbm' ● cursors : 'arrow', 'man', 'star',... ● Geometry strings : 'wxh±x±y' Canvas ● create_arc(), create_bitmap(), create_image(), create_line(), create_oval(),create_polygon(), create_rectangle, create_text(), create_window() ● 2 systèmes de coordonnées ● Listes 'above' et 'below' ● ID retourné par le constructeur ● Les tags Les classes de widgets Tkinter ● Button ● Menu ● Canvas ● Menubutton ● Checkbutton ● Message ● Entry ● Radiobutton ● Frame ● Scale ● Label ● Scrollbar ● Listbox ● Text ● Toplevel Les variables de contrôle ● Variable partageable entre widgets ● DoubleVar(), IntVar(), StringVar() ● Méthodes : set(valeur) et get()