'''
Bonjour,
Je suis confronté au problème suivant dans PyCharm 2024.1.4.:
Dans une fenêtre, on pose un Frame nommé option_frame dans lequel sont insérés des boutons ttk.Button créés par la classe Option(ttk.Button).
Un click sur un des boutons (option) doit faire apparaitre un messagebox un message indiquant l'index du bouton cliqué.
PROBLEMES: les paramètres des boutons créés ne sont pas visualisés (exemple: text et couleurs) ou fonctionnels comme l'attribut command.
Remarques:
L'objectif est de créer un menu qui permettra d'ouvrir un frame propre à chaque bouton dans main_frame.
Chaque frame correspondant à une option du menu sera créé dans un fichier individuel qui sera inclus dans ce code lors de l'appel de l'option.
MERCI PAR AVANCE POUR VOTRE AIDE
'''
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
root = tk.Tk()
root.geometry('500x400')
root.title('Application')
option_frame = tk.Frame(root, bg='#c3c3c3')
main_frame = tk.Frame(root, bg='#a0a0a0', highlightbackground='black', highlightthickness=2)
def msg(index):
messagebox.showwarning('Confirmation', 'Frame supprimé : ' + str(index))
class Option(ttk.Button):
def __init__(self,index):
super().__init__(master=option_frame)
self.index = index
self.text = 'Option: '+str(index)
self.place(x=10, y=40*(index+1))
self.font = ('Bold', 4)
self.fg = '#158aff'
self.bg = '#c3c3c3'
self.activebackground = '#c3c3c3'
self.command = lambda: msg(self.index)
option_frame.pack(side=tk.LEFT)
option_frame.pack_propagate(False)
option_frame.configure(width=100, height=400)
main_frame.pack_propagate(False)
main_frame.configure(width=500, height=400)
main_frame.pack(side=tk.LEFT)
Nb = 4
for i in range(Nb):
Option(i)
root.mainloop()
Partager