I have an application made with Python 2.7.x and Tkinter that, among other things, has the opening of a widget Toplevel
as a Messagebox
custom.
More or less, the code that follows is what the Toplevel in question is built in (some of the parameters are sent from the main window of the application, such as the width ( w ) , height ( h ), indicator to know which language file to import to display the texts in the appropriate language (lng_app), configuration options such as colors (_cfg__), etc ...):
# encoding: utf-8
# Tkinter
from libs._import_ import tk, tkFont
_cfg__ = ''
lng = ''
class _tpLvl_MsgBx_(tk.Toplevel):
'''Ventana secundaria para efectuar la salida del programa.'''
def __init__(self, parent=None, lng_app=None, _cfg__app=None, _w_='', _h_='', _x_='', _y_='', _tit_app='', _img_logo_app='', msg_type='', msg_txt='', btn_conf_01_txt=None, btn_conf_02_txt=None, extra_design=None, *args, **kwargs):
tk.Toplevel.__init__(self, parent, *args, **kwargs)
global _cfg__, lng
_cfg__ = _cfg__app
lng = lng_app
self.parent = parent
self._img_logo_app = _img_logo_app
self.msg_txt = msg_txt
_w_a_elim_ = 187
self.msg_box_msg_w = _w_ - _w_a_elim_
# Fuente por defecto para los Message
#{'family': 'Arial', 'weight': 'normal', 'slant': 'roman', 'overstrike': 0, 'underline': 0, 'size': 10} self.mainfont.actual()
self.font_x_defecto = tkFont.Font(family='Consolas', size=10, weight='bold')
img_msg_icons = {
'info': 'img/icon-info.png',
'confirm': 'img/icon-confirm.png',
'warning': 'img/icon-warning.png',
'error': 'img/icon-error.png',
}
if(msg_type == 'info'):
msg_tit = lng.data['msgb_tit_info']
elif(msg_type == 'confirm'):
msg_tit = lng.data['msgb_tit_confirm']
elif(msg_type == 'warning'):
msg_tit = lng.data['msgb_tit_warning']
elif(msg_type == 'error'):
msg_tit = lng.data['msgb_tit_error']
####msg_tit += _tit_app
msg_tit += 'Viajes'
self.img_msg_icon = tk.PhotoImage(file=img_msg_icons[msg_type])
self.geometry('{}x{}+{}+{}'.format(_w_, _h_, _x_, _y_))
# lng >> Título del panel según el caso
self.title(msg_tit)
self.config(bg=_cfg__._root_color_blanco, padx=5, pady=5)
self.resizable(1,1)
self.msg_box_frm_content_main = tk.Frame(self, bg=_cfg__._root_color_quater, padx=5, pady=5)
self.msg_box_frm_content_main.pack(fill='both', expand=1)
self.msg_box_frm_content = tk.Frame(self.msg_box_frm_content_main, bg=_cfg__._root_color_quater, padx=5, pady=5)
self.msg_box_frm_content.grid(row=0, column=1)
self.msg_box_frm_btns = tk.Frame(self.msg_box_frm_content_main, bg=_cfg__._root_color_quater, padx=2, pady=5)
self.msg_box_frm_btns.grid(row=1, column=1)
if(extra_design is None):
msg_box_logo = tk.Label(self.msg_box_frm_content_main, image=self._img_logo_app)
msg_box_logo.grid(row=0, column=0, rowspan=2, padx=3)
msg_box_logo = tk.Label(self.msg_box_frm_content, bg=_cfg__._root_color_quater, image=self.img_msg_icon)
msg_box_logo.grid(row=0, column=0, sticky='n')
# lng >> Mensaje a Mostrar
msg_box_msg = tk.Message(self.msg_box_frm_content, text=self.msg_txt, bg=_cfg__._root_color_quater, padx=10, pady=0)
msg_box_msg.grid(row=0, column=1)
msg_box_msg.config(width=self.msg_box_msg_w, anchor='center', font=self.font_x_defecto)
else:
if(extra_design == 'info_tot_viajes'):
##self.content_info_tot_viajes
msg_box_logo = tk.Label(self.msg_box_frm_content_main, image=self._img_logo_app)
msg_box_logo.grid(row=0, column=0, rowspan=2, padx=3)
msg_box_logo = tk.Label(self.msg_box_frm_content, bg=_cfg__._root_color_quater, image=self.img_msg_icon)
msg_box_logo.grid(row=0, column=0, sticky='n')
# lng >> Mensaje a Mostrar
msg_box_msg = tk.Message(self.msg_box_frm_content, text=self.msg_txt['tot_viajes'], bg=_cfg__._root_color_quater, padx=10, pady=0)
msg_box_msg.grid(row=0, column=1)
msg_box_msg.config(width=self.msg_box_msg_w, anchor='center', font=self.font_x_defecto)
msg_box_frm_sub_content = tk.Frame(self.msg_box_frm_content, bg='yellow', bd=2, width=1000)
msg_box_frm_sub_content.grid(row=1, column=0, columnspan=2)
print 'msg_box_frm_sub_content.config() >>', msg_box_frm_sub_content.config()
print 'self.msg_box_msg_w >>', self.msg_box_msg_w
self.msg_box_msg_w = 500
print 'self.msg_box_msg_w >>', self.msg_box_msg_w
msg_box_bar_tit = tk.Message(msg_box_frm_sub_content, text=self.msg_txt['tot_bar'][0], bg=_cfg__._root_color_quater, padx=5, pady=0)
msg_box_bar_tit.grid(row=0, column=0, sticky='w')
msg_box_bar_tit.config(width=self.msg_box_msg_w, font=self.font_x_defecto)
msg_box_bar_tot = tk.Message(msg_box_frm_sub_content, text=self.msg_txt['tot_bar'][1], bg=_cfg__._root_color_quater, padx=5, pady=0)
msg_box_bar_tot.grid(row=0, column=1, sticky='e')
msg_box_bar_tot.config(width=self.msg_box_msg_w, font=self.font_x_defecto)
msg_box_hab_tit = tk.Message(msg_box_frm_sub_content, text=self.msg_txt['tot_hab'][0], bg=_cfg__._root_color_quater, padx=5, pady=0)
msg_box_hab_tit.grid(row=1, column=0, sticky='w')
msg_box_hab_tit.config(width=self.msg_box_msg_w, font=self.font_x_defecto)
msg_box_hab_tot = tk.Message(msg_box_frm_sub_content, text=self.msg_txt['tot_hab'][1], bg=_cfg__._root_color_quater, padx=5, pady=0)
msg_box_hab_tot.grid(row=1, column=1, sticky='e')
msg_box_hab_tot.config(width=self.msg_box_msg_w, font=self.font_x_defecto)
msg_box_par_tit = tk.Message(msg_box_frm_sub_content, text=self.msg_txt['tot_par'][0], bg=_cfg__._root_color_quater, padx=5, pady=0)
msg_box_par_tit.grid(row=2, column=0, sticky='w')
msg_box_par_tit.config(width=self.msg_box_msg_w, font=self.font_x_defecto)
msg_box_par_tot = tk.Message(msg_box_frm_sub_content, text=self.msg_txt['tot_par'][1], bg=_cfg__._root_color_quater, padx=5, pady=0)
msg_box_par_tot.grid(row=2, column=1, sticky='e')
msg_box_par_tot.config(width=self.msg_box_msg_w, font=self.font_x_defecto)
msg_box_ven_tit = tk.Message(msg_box_frm_sub_content, text=self.msg_txt['tot_ven'][0], bg=_cfg__._root_color_quater, padx=5, pady=0)
msg_box_ven_tit.grid(row=3, column=0, sticky='w')
msg_box_ven_tit.config(width=self.msg_box_msg_w, font=self.font_x_defecto)
msg_box_ven_tot = tk.Message(msg_box_frm_sub_content, text=self.msg_txt['tot_ven'][1], bg=_cfg__._root_color_quater, padx=5, pady=0)
msg_box_ven_tot.grid(row=3, column=1, sticky='e')
msg_box_ven_tot.config(width=self.msg_box_msg_w, font=self.font_x_defecto)
msg_box_frm_sub_content.grid_columnconfigure(0, weight=1)
msg_box_frm_sub_content.grid_columnconfigure(1, weight=1)
msg_box_frm_sub_content.grid_rowconfigure(0, weight=1)
msg_box_frm_sub_content.grid_rowconfigure(1, weight=1)
msg_box_frm_sub_content.grid_rowconfigure(2, weight=1)
msg_box_frm_sub_content.grid_rowconfigure(3, weight=1)
if(msg_type == 'info' or msg_type == 'warning' or msg_type == 'error'):
# lng >> 'Aceptar'
msg_box_btn_ok = tk.Button(self.msg_box_frm_btns, text=lng.data['btn_ok'], font=('Consolas', 8), width=5, height=1, command=self.destroy)
msg_box_btn_ok.grid(row=0, column=0)
elif(msg_type == 'confirm'):
if(btn_conf_01_txt is not None and btn_conf_02_txt is not None):
btn_opc_01_txt = btn_conf_01_txt
btn_opc_02_txt = btn_conf_02_txt
else:
btn_opc_01_txt = lng.data['btn_si']
btn_opc_02_txt = lng.data['btn_no']
# lng >> 'Si' u Opción-01
msg_box_btn_ok = tk.Button(self.msg_box_frm_btns, text=btn_opc_01_txt, font=('Consolas', 8), width=5, height=1, command=lambda: self.confirm_return(1))
msg_box_btn_ok.grid(row=0, column=0, padx=5)
# lng >> 'No' u Opción-02
msg_box_btn_cncl = tk.Button(self.msg_box_frm_btns, text=btn_opc_02_txt, font=('Consolas', 8), width=5, height=1, command=lambda: self.confirm_return(0))
msg_box_btn_cncl.grid(row=0, column=1, padx=5)
def confirm_return(self, _return):
'''Devolviendo respuesta de confirmación.'''
self.parent.confirm_var.set(_return)
self.destroy()
def content_info_tot_ent(self):
'''Contenido personalizado para el panel informativo
de total de entidades tratadas.'''
msg_box_logo = tk.Label(self.msg_box_frm_content_main, image=self._img_logo_app)
msg_box_logo.grid(row=0, column=0, rowspan=2, padx=3)
msg_box_logo = tk.Label(self.msg_box_frm_content, bg=_cfg__._root_color_quater, image=self.img_msg_icon)
msg_box_logo.grid(row=0, column=0, sticky='n')
# lng >> Mensaje a Mostrar
msg_box_msg = tk.Message(self.msg_box_frm_content, text=self.msg_txt, bg=_cfg__._root_color_quater, padx=10, pady=0)
msg_box_msg.grid(row=0, column=1)
msg_box_msg.config(width=self.msg_box_msg_w, anchor='center', font=self.font_x_defecto)
This Toplevel
, as I said, is a kind of personalized Messagebox for the typical messages of information, alert, error and confirmation. For special cases that fall outside of a design and / or standard measurements, it is indicated by the extra_design
In the case that is exposed, there comes the case in which the extra_design='info_tot_viajes'
is demanded. In that case, there is a Frame
called msg_box_frm_sub_content
that occupies one of the Frame cells self.msg_box_frm_content
. The mentioned msg_box_frm_sub_content
is divided into two columns and four rows.
The problem I have is that Frame msg_box_frm_sub_content
does not get to expand all the necessary throughout its assigned cell within self.msg_box_frm_content
.
This is what it looks like right now:
(I have left a yellow background in msg_box_frm_sub_content
to see what it occupies but, in the end, it would have the same blue color as its parent Frame).
The thing is that what I intend is to expand or across the width of the cell it occupies or, at least as it appears in this other image:
To make the distribution of the Frames (or tables with their rows and columns) clearer, there is another illustrative image:
Well, that's it, I've tried several ways to get the msg_box_frm_sub_content
to expand but it does not get it in any way.
I have tried to expand the Frame
with the property of grid_rowconfigure()
but nothing.
Even, I increase the width, increase and increase of value but nothing changes its width size and remains the same, I put the value that I put.
So, can someone show me where I have the fault?
On the other hand, apart from the problem of widths and how to expand the Frame properly, another question arose in the same code.
As you can see in the code, inside the conditional:
if(extra_design is None):
else:
if(extra_design == 'info_tot_viajes'):
##self.content_info_tot_viajes
when the if(extra_design == 'info_tot_viajes'):
is met, self.content_info_tot_viajes
is called (now it is commented because the content of the called method is not loaded).
I do not know why the simple call to the method does not load the content built into it. Because I do not get to upload I commented the call to the method and I have put the content of the method within else
.
Why is it not enough to simply call the method to upload its content within else
?
Thanks in advance for the possible solution suggestions.