Créer un bouton retour sur tkinter
Résolu
Anonymus
-
Anonymus -
Anonymus -
Bonjour à tous,
Je développe actuellement une application en Python, et je voulais savoir comment créer un bouton "retour" dans une interface graphique avec Tkinter.
Merci
Je développe actuellement une application en Python, et je voulais savoir comment créer un bouton "retour" dans une interface graphique avec Tkinter.
Merci
A voir également:
- Bouton tkinter command
- Sfc scannow command - Guide
- Bouton reinitialisation pc - Guide
- Diagnostic bouton photo - Accueil - Outils
- Bouton on/off comment savoir ✓ - Forum Matériel & Système
- Symbole arrêt marche - Forum Word
4 réponses
Pour les curieux, j'ai résolu mon problème en utilisant le widget toplevel:
Voici mon code:
Bonne journée(matin,soirée..)
Voici mon code:
# -*- coding: utf-8 -* """ Application: Calcules les aires des caré, rectangle et rond """ #On importe les modules import tkinter from tkinter import * import math from math import * import tkinter.messagebox from tkinter.messagebox import * from tkinter import ttk import webbrowser #on créer la fenetre window=Tk() window.geometry("689x720") #Dimension par défault window.minsize(689, 720) # Dimension minimale window.maxsize(800, 800) # Dimension maximal window.config(background='dodgerblue4') # couleur de fond de la fênetre window.title("Calculer des aires") # titre de la fenetre #On créer les Frames FramePrincipale = Frame(window) # On place la frame dans la fentre FramePrincipale.config(background ='dodgerblue4') # couleur de fond de la frame FramePrincipale.pack(anchor=CENTER, expand="YES") # fonction d'expansion Frame1 = Frame(FramePrincipale, bd=5, relief=SUNKEN) Frame1.pack(anchor="n") Frame2 = Frame(FramePrincipale, bd=5, relief=SUNKEN) Frame2.pack(anchor="center") Frame3 = Frame(FramePrincipale, bd=5, relief=SUNKEN) Frame3.pack(anchor="s") FrameBoutons = Frame(window, bd=5, relief=GROOVE) FrameBoutons.pack(side=BOTTOM) #On définit les commandes pour les boutons def aire_carré(): window_carre = Toplevel(window) window_carre.config(background='dodgerblue4') window_carre.geometry("600x500") def back_carre(): window_carre.destroy() def calculecarré(): C = int(Coté.get()) Ccarré = C ** 2 FrameResultat = Frame(window_carre, bg="dodgerblue4") LabelReponse = Label(FrameResultat, text=Ccarré, bg='dodgerblue4', fg='red') LabelReponse.grid(column=0, row=0) bouton1.config(state = DISABLED) listeProduits=["Votre valeur :", "mm²","cm²","dm²", "m²", "dam²", "hm²", "km²"] listeCombo = ttk.Combobox(FrameResultat, values=listeProduits) listeCombo.current(0) listeCombo.grid(column=1, row=0) FrameResultat.pack(expand=YES) bouton_back=Button(window_carre, text="Retour", command=back_carre, cursor="plus", background="red", activebackground="orange") FramePrinciaple2 = Frame(window_carre, bd=5, relief=RIDGE, bg='dodgerblue4') Coté = Entry(FramePrinciaple2, bg='red') Info = Label(FramePrinciaple2, text="Entrez la longueur d'un coté du carré(nombre entier)", bg='dodgerblue4') bouton1 = Button(window_carre, text="Calculer", command=calculecarré) FramePrinciaple2.pack(side=TOP, expand=YES) Coté.pack(expand=YES) bouton1.pack(expand=YES) Info.pack(expand=YES) bouton_back.pack(side=BOTTOM) def aire_rectangle(): window_rec = Toplevel(window) window_rec.config(background='dodgerblue4') window_rec.geometry("600x500") def back_rec(): window_rec.destroy() def calculerectangle(): L = int(Largeur.get()) H = int(Hauteur.get()) Calcule = L * H FrameResultat = Frame(window_rec, bg="dodgerblue4") LabelReponse = Label(FrameResultat, text=Calcule, fg='red', bg='dodgerblue4') LabelReponse.grid(column=0, row=0) listeProduits=["Votre valeur :", "mm²","cm²","dm²", "m²", "dam²", "hm²", "km²"] listeCombo = ttk.Combobox(FrameResultat, values=listeProduits) listeCombo.current(0) listeCombo.grid(column=1, row=0) FrameResultat.pack(expand=YES) bouton1.config(state = DISABLED) bouton_back=Button(window_rec, text="Retour", command=back_rec, cursor="plus", background="red", activebackground="orange") FramePrinciaple2 = Frame(window_rec, bd=5, relief=RIDGE, bg='dodgerblue4') FramePrincipale3 = Frame(window_rec, bd=5, relief=RIDGE, bg='dodgerblue4') Largeur = Entry(FramePrinciaple2, bg='red', fg='white') Hauteur = Entry(FramePrincipale3, bg='red', fg='white') InfoHauteur = Label(FramePrincipale3, text="Entrez la longeur du rectangle(nombre entier)", bg='dodgerblue4') InfoLargeur = Label(FramePrinciaple2, text="Entrez la largeur du rectangle(nombre entier)", bg='dodgerblue4') bouton1 = Button(window_rec, text="Calculer", command=calculerectangle) FramePrinciaple2.pack(side=TOP, expand=YES) FramePrincipale3.pack(side = BOTTOM, expand=YES) Largeur.pack(expand=YES) Hauteur.pack(expand=YES) InfoHauteur.pack(expand=YES) bouton1.pack(expand=YES) InfoLargeur.pack(expand=YES) bouton_back.pack(anchor="s") def aire_rond(): window_rond = Toplevel(window) window_rond.config(background='dodgerblue4') window_rond.geometry("600x500") def back_rond(): window_rond.destroy() def calculerond(): R = int(Rayon.get()) Calcule1 = R ** 2 Calcule2 = Calcule1 * pi Frameresultat = Frame(window_rond) Frameresultat.pack(expand=YES) LabelReponse = Label(Frameresultat, text=Calcule2, bg='dodgerblue4') LabelReponse.grid(column=0, row=0) listeProduits=["Votre valeur :", "mm²","cm²","dm²", "m²", "dam²", "hm²", "km²"] listeCombo = ttk.Combobox(Frameresultat, values=listeProduits) listeCombo.current(0) listeCombo.grid(column=1, row=0) bouton1.config(state = DISABLED) FramePrinciaple2 = Frame(window_rond, bd=5, relief=RIDGE, bg='dodgerblue4') InfoHauteur = Label(FramePrinciaple2, text="Entrez le rayon du cercle(nombre entier)", bg='dodgerblue4') Rayon = Entry(FramePrinciaple2, bg='black', fg='white') InfoHauteur.pack(expand=YES) bouton1 = Button(window_rond, text="Calculer", command=calculerond) bouton_back=Button(window_rond, text="Retour", command=back_rond, cursor="plus", background="red", activebackground="orange") bouton1.pack(expand=YES) bouton_back.pack(side=BOTTOM) FramePrinciaple2.pack(anchor="n", expand=YES) Rayon.pack(expand=YES) #On définit la commande pour le bouton Afficher formules def createNewWindow(): # On configure la fenetre new2 = Toplevel(window) new2.config(background='dodgerblue4') new2.geometry("2080x720") #On définit le carré #On créer la frame FrameCarré = Frame(new2, bg="dodgerblue4") FrameCarré.pack(side=TOP, expand=YES) #On définit le texte labelFormuleCarré = Label(FrameCarré, text="Pour le carré, la forumule est : coté au carré ou c²", font=("Helvetica", 20), bg='dodgerblue4') labelFormuleCarré.grid(column=0, row=0) #On créer le carré canvascarré = Canvas(FrameCarré, width=100, height=100, bg="dodgerblue4") canvascarré.grid(column=1, row=0) carré = canvascarré.create_rectangle(0,0,100,100, fill="orange") #On définit le rectangle #On créer la frame FrameRectangle = Frame(new2, bg="dodgerblue4") FrameRectangle.pack(anchor="center") #On définit le texte labelFormuleRectangle = Label(FrameRectangle, text="Pour le rectangle la formule est: largeur fois hauteur ou lxh", font=("Helvetica", 20), bg='dodgerblue4') labelFormuleRectangle.grid(column=0, row=0) # On dessine le rectangle canvasrec = Canvas(FrameRectangle, width=100, height=50, bg="dodgerblue4") canvasrec.grid(column=1, row=0) rond = canvasrec.create_rectangle(0,0,100,50, fill="dark turquoise") #On définit le rond #On créer la frame FrameRond = Frame(new2, bg="dodgerblue4") FrameRond.pack(side=BOTTOM, expand=YES) #On définit le texte labelForumleRond = Label(FrameRond, text="Pour le rond, la formule est: rayon au carré multiplié par π ou r²xπ", font=("Helvetica", 20), bg='dodgerblue4') labelForumleRond.grid(column=0, row=0) #On déssine le rond canvasrond = Canvas(FrameRond, width=100, height=100, bg="dodgerblue4") canvasrond.grid(column=1, row=0) rond = canvasrond.create_oval(0,0,100,100, fill="red") #On définit l'ouverture du site def openwebsite(): webbrowser.open_new("https://astucespourandroid.blogspot.com") #On définit la commande quitter def quitter(): MsgBox = messagebox.askquestion (title='Attention',message='Voulez-vous vraiement quitter l application?',icon = 'warning') if MsgBox == 'yes': window.destroy() #On créer les boutons bouton1 = Button(Frame1, text="Calculer aire carré", command=aire_carré, bg='#0032FF', fg='white') bouton2 = Button(Frame2, text="Calculer aire rectangle", command=aire_rectangle, bg='red', fg='white') bouton3 = Button(Frame3, text="Calculer aire rond", command=aire_rond, bg='black', fg='white') boutonFormules = Button(FrameBoutons, text="Afficher les Formules", command=createNewWindow) boutonQuitter = Button(FrameBoutons, text="Quitter", command=quitter) boutonMonSite = Button(FrameBoutons, text="Mon site web", command=openwebsite) boutonMonSite.grid(column=2, row=0,) boutonFormules.grid(column=1, row=0) boutonQuitter.grid(column=3,row=0 ) bouton1.pack() bouton2.pack() bouton3.pack() #On lance la fenetre window.mainloop()
Bonne journée(matin,soirée..)
Bonjour Anonymus,
Que fais ton application exactement ?
Et quelle action veux tu faire avec ton bouton "retour" ?
Que fais ton application exactement ?
Et quelle action veux tu faire avec ton bouton "retour" ?
Bonjour,
Je creer un app qui calcule des aires. J ai un menu principale où on choisit sa figure. Je voudrai creer un bouton retour qui me permettrais de revenir au menu principale ou on choisit sa figure.
J espère avoir était assez clair.
Je pensais a un code comme cela:
Je creer un app qui calcule des aires. J ai un menu principale où on choisit sa figure. Je voudrai creer un bouton retour qui me permettrais de revenir au menu principale ou on choisit sa figure.
J espère avoir était assez clair.
Je pensais a un code comme cela:
Button(window, text="retour", command=back
OK Anonymus,
on appelle donc la fonction 'back' qu'il faut, bien sur, définir dans ton appli:
Et donc dans cette fonction, on écrit le code pour revenir au menu, mais comme on ne sait pas comment tu as structuré ton appli, comment tu as défini ton menu, etc ...
on ne peut te répondre plus précisément
L'idéal est de copier ton code complet ici
b1 = Button(fenetre, text='Retour', command=back)
on appelle donc la fonction 'back' qu'il faut, bien sur, définir dans ton appli:
def back(): ''' retour au menu principal ''' ... mes instructions Python ...
Et donc dans cette fonction, on écrit le code pour revenir au menu, mais comme on ne sait pas comment tu as structuré ton appli, comment tu as défini ton menu, etc ...
on ne peut te répondre plus précisément
L'idéal est de copier ton code complet ici
Bonjour,
Vous trouverez mon code complet ci-dessous:
Je voudrais savoir quel sont les instructions python que je dois écrire dans la fonction back.
Vous trouverez mon code complet ci-dessous:
# -*- coding: utf-8 -* #On importe les modules import tkinter from tkinter import * import math from math import * import tkinter.messagebox from tkinter.messagebox import * from tkinter import ttk import webbrowser #on créer la fenetre window=Tk() window.geometry("689x720") #Dimension par défault window.minsize(689, 720) # Dimension minimale window.maxsize(800, 800) # Dimension maximal window.config(background='dodgerblue4') # couleur de fond de la fênetre window.title("Calculer des aires") # titre de la fenetre #On créer les Frames FramePrincipale = Frame(window) # On place la frame dans la fentre FramePrincipale.config(background ='dodgerblue4') # couleur de fond de la frame FramePrincipale.pack(anchor=CENTER, expand="YES") # fonction d'expansion Frame1 = Frame(FramePrincipale, bd=5, relief=SUNKEN) Frame1.pack(anchor="n") Frame2 = Frame(FramePrincipale, bd=5, relief=SUNKEN) Frame2.pack(anchor="center") Frame3 = Frame(FramePrincipale, bd=5, relief=SUNKEN) Frame3.pack(anchor="s") FrameBoutons = Frame(window, bd=5, relief=GROOVE) FrameBoutons.pack(anchor="nw") # on définit le bouton retour #On définit les commandes pour les boutons def aire_carré(): def calculecarré(): C = int(Coté.get()) Ccarré = C ** 2 FrameResultat = Frame(window, bg="dodgerblue4") LabelReponse = Label(FrameResultat, text=Ccarré, bg='dodgerblue4', fg='red') LabelReponse.grid(column=0, row=0) bouton1.config(state = DISABLED) listeProduits=["Votre valeur :", "mm²","cm²","dm²", "m²", "dam²", "hm²", "km²"] listeCombo = ttk.Combobox(FrameResultat, values=listeProduits) listeCombo.current(0) listeCombo.grid(column=1, row=0) FrameResultat.pack(expand=YES) FramePrincipale.destroy() boutonretour.config(state=NORMAL) FramePrinciaple2 = Frame(window, bd=5, relief=RIDGE, bg='dodgerblue4') Coté = Entry(FramePrinciaple2, bg='red') Info = Label(FramePrinciaple2, text="Entrez la longueur d'un coté du carré(nombre entier)", bg='dodgerblue4') bouton1 = Button(window, text="Calculer", command=calculecarré) FramePrinciaple2.pack(side=TOP, expand=YES) Coté.pack(expand=YES) bouton1.pack(expand=YES) Info.pack(expand=YES) def aire_rectangle(): def calculerectangle(): L = int(Largeur.get()) H = int(Hauteur.get()) Calcule = L * H FrameResultat = Frame(window, bg="dodgerblue4") LabelReponse = Label(FrameResultat, text=Calcule, fg='red', bg='dodgerblue4') LabelReponse.grid(column=0, row=0) listeProduits=["Votre valeur :", "mm²","cm²","dm²", "m²", "dam²", "hm²", "km²"] listeCombo = ttk.Combobox(FrameResultat, values=listeProduits) listeCombo.current(0) listeCombo.grid(column=1, row=0) FrameResultat.pack(expand=YES) bouton1.config(state = DISABLED) FramePrincipale.destroy() boutonretour.config(state=NORMAL) FramePrinciaple2 = Frame(window, bd=5, relief=RIDGE, bg='dodgerblue4') FramePrincipale3 = Frame(window, bd=5, relief=RIDGE, bg='dodgerblue4') Largeur = Entry(FramePrinciaple2, bg='red', fg='white') Hauteur = Entry(FramePrincipale3, bg='red', fg='white') InfoHauteur = Label(FramePrincipale3, text="Entrez la longeur du rectangle(nombre entier)", bg='dodgerblue4') InfoLargeur = Label(FramePrinciaple2, text="Entrez la largeur du rectangle(nombre entier)", bg='dodgerblue4') bouton1 = Button(window, text="Calculer", command=calculerectangle) FramePrinciaple2.pack(side=TOP, expand=YES) FramePrincipale3.pack(side = BOTTOM, expand=YES) Largeur.pack(expand=YES) Hauteur.pack(expand=YES) InfoHauteur.pack(expand=YES) bouton1.pack(expand=YES) InfoLargeur.pack(expand=YES) def aire_rond(): def calculerond(): R = int(Rayon.get()) Calcule1 = R ** 2 Calcule2 = Calcule1 * pi Frameresultat = Frame(window) Frameresultat.pack(expand=YES) LabelReponse = Label(Frameresultat, text=Calcule2, bg='dodgerblue4') LabelReponse.grid(column=0, row=0) listeProduits=["Votre valeur :", "mm²","cm²","dm²", "m²", "dam²", "hm²", "km²"] listeCombo = ttk.Combobox(Frameresultat, values=listeProduits) listeCombo.current(0) listeCombo.grid(column=1, row=0) bouton1.config(state = DISABLED) FramePrincipale.destroy() boutonretour.config(state=NORMAL) FramePrinciaple2 = Frame(window, bd=5, relief=RIDGE, bg='dodgerblue4') InfoHauteur = Label(FramePrinciaple2, text="Entrez le rayon du cercle(nombre entier)", bg='dodgerblue4') Rayon = Entry(FramePrinciaple2, bg='black', fg='white') InfoHauteur.pack(expand=YES) bouton1 = Button(window, text="Calculer", command=calculerond) bouton1.pack(expand=YES) FramePrinciaple2.pack(anchor="n", expand=YES) Rayon.pack(expand=YES) #On définit la commande pour le bouton Afficher formules def createNewWindow(): # On configure la fenetre new2 = Toplevel(window) new2.config(background='dodgerblue4') new2.geometry("2080x720") #On définit le carré #On créer la frame FrameCarré = Frame(new2, bg="dodgerblue4") FrameCarré.pack(side=TOP, expand=YES) #On définit le texte labelFormuleCarré = Label(FrameCarré, text="Pour le carré, la forumule est : coté au carré ou c²", font=("Helvetica", 20), bg='dodgerblue4') labelFormuleCarré.grid(column=0, row=0) #On créer le carré canvascarré = Canvas(FrameCarré, width=100, height=100, bg="dodgerblue4") canvascarré.grid(column=1, row=0) carré = canvascarré.create_rectangle(0,0,100,100, fill="orange") #On définit le rectangle #On créer la frame FrameRectangle = Frame(new2, bg="dodgerblue4") FrameRectangle.pack(anchor="center") #On définit le texte labelFormuleRectangle = Label(FrameRectangle, text="Pour le rectangle la formule est: largeur fois hauteur ou lxh", font=("Helvetica", 20), bg='dodgerblue4') labelFormuleRectangle.grid(column=0, row=0) # On dessine le rectangle canvasrec = Canvas(FrameRectangle, width=100, height=50, bg="dodgerblue4") canvasrec.grid(column=1, row=0) rond = canvasrec.create_rectangle(0,0,100,50, fill="dark turquoise") #On définit le rond #On créer la frame FrameRond = Frame(new2, bg="dodgerblue4") FrameRond.pack(side=BOTTOM, expand=YES) #On définit le texte labelForumleRond = Label(FrameRond, text="Pour le rond, la formule est: rayon au carré multiplié par π ou r²xπ", font=("Helvetica", 20), bg='dodgerblue4') labelForumleRond.grid(column=0, row=0) #On déssine le rond canvasrond = Canvas(FrameRond, width=100, height=100, bg="dodgerblue4") canvasrond.grid(column=1, row=0) rond = canvasrond.create_oval(0,0,100,100, fill="red") #On définit l'ouverture du site def openwebsite(): webbrowser.open_new("https://astucespourandroid.blogspot.com") #On définit la commande quitter def quitter(): MsgBox = messagebox.askquestion (title='Attention',message='Voulez-vous vraiement quitter l application?',icon = 'warning') if MsgBox == 'yes': window.destroy() #On créer les boutons bouton1 = Button(Frame1, text="Calculer aire carré", command=aire_carré, bg='#0032FF', fg='white') bouton2 = Button(Frame2, text="Calculer aire rectangle", command=aire_rectangle, bg='red', fg='white') bouton3 = Button(Frame3, text="Calculer aire rond", command=aire_rond, bg='black', fg='white') boutonretour = Button(FrameBoutons, text="Retour") boutonFormules = Button(FrameBoutons, text="Afficher les Formules", command=createNewWindow) boutonQuitter = Button(FrameBoutons, text="Quitter", command=quitter) boutonMonSite = Button(FrameBoutons, text="Mon site web", command=openwebsite) boutonMonSite.grid(column=2, row=0,) boutonFormules.grid(column=1, row=0) boutonretour.grid(column=0, row=0) boutonQuitter.grid(column=3,row=0 ) bouton1.pack() bouton2.pack() bouton3.pack() boutonretour.config(state = DISABLED) #On lance la fenetre window.mainloop()
Je voudrais savoir quel sont les instructions python que je dois écrire dans la fonction back.